我在两个实体之间有一个OneToMany关系:商店和评论。我想显示在收到的最新评论之前订购的商店的列表。 这是我到目前为止尝试过的:
return $this->createQueryBuilder('s')
->innerJoin('s.reviews', 'r', 'WITH', 'r.shop = s.id')
->orderBy('r.createdAt') // <- that does nothing on the order of the shops
->getQuery()
->getResult();
这将退回所有商店,但根本没有订购这些商品...
答案 0 :(得分:1)
如果您想在ShopRepository中使用它,请尝试这种方式
return $this->getEntityManager()
->createQueryBuilder()
->select('s, MAX(r.createdAt) AS maxCreatedAt')
->from('AppBundle:Comment', 'r')
->join('AppBundle:Shop', 's', 'WITH', 'r.shop = s.id')
->groupBy('s')
->orderBy('maxCreatedAt', 'DESC')
->getQuery()
->getResult();
不要忘记相应地修改包名称空间(AppBundle)。
答案 1 :(得分:0)
尝试像这样直接在您的OrderBy
关系上设置@ORM\OneToMany mappedBy
:
/**
* @ORM\OneToMany...
* @ORM\OrderBy({"createdAt" = "DESC"})
*/
private $reviews;