使用EXISTS查询Doctrine Symfony2

时间:2022-08-31 06:45:21

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?

没有检查的部分很容易,但有没有办法实现EXISTS?

1 个解决方案

#1


24  

You either need to use two query builders:

您需要使用两个查询构建器:

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

or use DQL directly:

或直接使用DQL:

$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://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html

您将在文档中找到更多示例:http://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html

#1


24  

You either need to use two query builders:

您需要使用两个查询构建器:

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

or use DQL directly:

或直接使用DQL:

$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://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html

您将在文档中找到更多示例:http://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html