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()))
            ->setParameter(':type', 'product')
            ->setParameter(':fieldName', $fieldName)
            ->setParameter(':value', $value)
            ->getQuery()
            ->execute();

Comments

Popular posts from this blog

Idea PhpStorm - How to fix bugs with incorrect code Inspections and Autocomplete

CSS Как прикрепить слой (div) к краю

PHP - How to show expected return array elements