22

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?

4

1 回答 1

34

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

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

或直接使用 DQL:

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

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

于 2012-04-05T15:48:42.047 回答