2011-10-18 42 views
1

我正试图从我的数据库中获得足够床位的酒店(用户指定客人计数参数)。查询应该看起来像这样:Doctrine2带SUM的QueryBuilder子查询?

SELECT h.* FROM Hotel AS h 
WHERE 
    (SELECT SUM(r.guestCount * r.count) 
    FROM Room AS r 
    WHERE r.hotel_id = h.id) >= $questCount 

上述查询包含where子句中的子查询。我读过doctrine的QueryBuilder文档,我不知道如何在QB中创建子查询。

所有我现在是:

$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder() 
    ->select('h') 
    ->from('AAAHotelsBundle:Hotel', 'h') 
    ->where(.........???...........); 

任何想法下一步该怎么做?

当然我简化了这个问题(查询本身更复杂)。我使用Symfony2。

回答

0

我认为这DQL将帮助您

SELECT h, SUM(r.guestCount * r.count) as TSUM FROM Hotel h JOIN h.room r 
WHERE TSUM >= :questCount 
2

DQL是不是在我的情况的解决方案。我确实需要使用QueryBuilder。 我有asked the same question on Google Groups这里是解决方案:

$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder() 
    ->select('h') 
    ->from('AAAHotelsBundle:Hotel', 'h') 
    ->join('Room', 'r') 
    ->groupBy('h') 
    ->having('SUM(r.guestCount * r.count) >= :guestCount') 
    ->setParameter("guestCount", $guestCount);