0

如何使用学说查询生成器编写以下内容?

SELECT
    registry.id ,
    registry.couple_id,
    registry.gift_id,
    Sum(purchase.amount) as totalContribution,
    gift.title,
    gift.price
FROM
    gift,
    registry
LEFT JOIN
    purchase ON purchase.registry_id = registry.id
WHERE
    registry.gift_id = gift.id
    AND registry.couple_id = 1 
GROUP BY
    registry.couple_id,
    registry.gift_id

我试过了:

$qb = $this->createQueryBuilder('g') //gift
    ->from('\BBB\GiftBundle\Entity\Registry', 'reg')
    ->select('g.id , g.title, g.description, g.price, g.imageMedium')
    ->addSelect('SUM(p.amount) as totalContribute')
    ->leftJoin('\BBB\GiftBundle\Entity\Purchase', 'p', 'ON','reg.id = p.registry')
    ->where('reg.gift = g.id')
    ->AndWhere('reg.couple = :coupleID')
    ->orderBy('reg.id','DESC')
    ->groupBy('reg.couple')
    ->groupBy('reg.gift')
    ->setParameter('coupleID', $coupleID);

但它给了我以下错误:

[语义错误] 第 0 行,第 178 列,靠近“p ON reg.id =”:错误:标识变量 BBB\GiftBundle\Entity\Purchase 用于连接路径表达式,但之前未定义。

4

1 回答 1

0

DQL 只能连接与当前实体(Registry位于此 DQL 中)相关的表。您的Registry实体将需要具有已声明的 OneToMany 关系。

假设这个关系被称为purchases你会像这样加入:

->leftJoin('reg.purchases', 'p')
于 2012-10-21T00:28:08.703 回答