Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query with EXISTS for Doctrine Symfony2

How can I implement the following query with Query Builder?

SELECT * 
FROM t 
WHERE t.status = 1
    OR EXISTS(SELECT * 
              FROM r 
              WHERE r.t_id = t.id 
                  AND r.status = 1
             )

The part without exist check is easy, but is there a way to implement the EXISTS?

like image 391
ArVan Avatar asked Apr 05 '12 14:04

ArVan


1 Answers

You either need to use two query builders:

$queryBuilder->expr()->exists($subQueryBuilder->getDql());

or use DQL directly:

$queryBuilder->expr()->exists('SELECT * 
    FROM r 
    WHERE r.t_id = t.id 
    AND r.status = 1'
);

You'll find more examples in the docs: http://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/query-builder.html

like image 178
Jakub Zalas Avatar answered Oct 18 '22 09:10

Jakub Zalas