Posts

Showing posts with the label Doctrine

Symfony - Doctrine - How to save SQL query to Session

If you have separated ajax table sheet with data and filters, sometimes, you need to keep this filtered data between requests. In my case, I needed to be able to download file in CSV format, after choosing all necessary filters in table. So, it is possible to store SQL query in client Session after applying all filters and receive data from DB via DQL. To store DQL in session: $query = $this->createQueryBuilder('i')->select('COUNT(i)'); $_SESSION['query'] = $query->getQuery()->getDQL(); $_SESSION['query_params'] = $query->getQuery()->getParameters(); To get data from DB: $data = $this->createQuery($_SESSION['query'])->setParameters($_SESSION['query_params'])->getResult();

Symfony - Doctrine - How to set one-to-one relation with field not exist in target table, but exist in another one

Today, I faced with a bug: Custom Field entity has a RelId column relates to two different entities: Product and Bundle. Set RelId into Custom Field directly connected to Product, but not exist in Bundle table. After Entity Manager flush(), I got an error: RelId can not be null. How come that RelId field became null, when flushing. After debugging, I realized, Doctrine checks if exist this connected RelId field in Bundle table. If not, Doctrine just set RelId to null and go on. So, we need not to set RelId directly, but using entity, like Product or Bundle, in my case. Here's a simplified example using annotations: Let's assume you have three entities: SourceEntity, TargetEntity, and RelationEntity. RelationEntity will act as the join table, holding the relationship between SourceEntity and TargetEntity. // SourceEntity.php use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity */ class SourceEntity { // ... other properties /** * @ORM\OneTo...

Symfony - Doctrine - How to change old attributes in entity to new PHP 8 attributes

Old-Style Annotations: Written as comments, typically using PHPDoc syntax (/** ... */). Annotations are interpreted by external tools or libraries. Doctrine ORM interprets annotations to configure database mappings. Limited flexibility in terms of syntax and available metadata types. May lack type safety and validation. PHP 8 Attributes: Written using the #[...] syntax. Introduced as a language feature in PHP 8 for adding metadata directly to code. Provides a more structured and standardized way of associating metadata with classes, methods, or properties. Attributes are a built-in language feature, eliminating the need for external interpretation in many cases. Attributes are directly handled by the PHP runtime. Offers better type safety and validation compared to traditional annotations. Supports the use of PHP's native types and expressions within attribute values. Attributes support namespacing, allowing for better organization and...

Symfony - Doctrine - How to add subquery into where clause

If you want to add subquery into where clause, you need to create subquery firstly: $sub = $this->_em->createQueryBuilder()             ->select('cf.id')             ->from(CustomField::class, 'cf')             ->where('cf.type = :type AND cf.fieldName = :fieldName'); After that you can use this subquery as DQL: $this->_em->createQueryBuilder()->expr()->in('cfv.fieldId', $sub->getDQL()) In that case we use it as IN condition. But it is just example. And finally it will look like that: $this->createQueryBuilder('cfv')             ->update(CustomFieldValue::class, 'cfv')             ->set('cfv.value', ':value')             ->where($this->_em->createQueryBuilder()->expr()->in('cfv.fieldId', $sub->getDQL()))           ...