两个使用DQL / Doctrine在select语句中计数

时间:2013-08-09 16:39:25

标签: jquery sql symfony count doctrine-orm

我有两个表产品和评论,

我希望用DQL /使用symfony进行查询。

通常我可以这样做:

// Get number of comment not visited per product
Select count (p.id) from Product p, Comment c where P.id = c.p_id and c.status = 0;

// Get number of comment read visited per product
Select count (p.id) from Product p, Comment c where P.id = c.p_id and c.status = 1;

但是,虽然即时通讯使用分页包我需要在参数中传递一个查询(比如我也使用过滤器包也需要只有一个查询,这样他就可以在其上应用过滤器了。)

任何想法!!

1 个答案:

答案 0 :(得分:0)

以下是解决方案:https://stackoverflow.com/a/14294335/875519

根据您的情况,这应该可以解决问题:

$this->getEntityManager()->createQuery('
    SELECT COUNT(p1) AS p1, COUNT(p2) AS p2
    FROM YourBundle:Product p1, YourBundle:Product p2, YourBundle:Comment c1, YourBundle:Comment c2
    WHERE p1.id = c1.p_id AND c1.status = 0
    AND p2.id = c2.p_id AND c2.status = 1
');