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);
但它給了我以下錯誤:
[Semantical Error] line 0, col 178 near 'p ON reg.id =': Error: Identification Variable BBB\GiftBundle\Entity\Purchase used in join path expression but was not defined before.
感謝您的答覆,但我只有一個多對一的關係,1個註冊表有許多購買。如何爲這種情況編寫它? –
如果你不想有雙向關係,那麼你需要編寫一個[原生SQL查詢](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/參考/天然-sql.html)。 – Alex