我有一个实体 Book,它与其他实体的关系太多。现在我想对多对多关系属性之一的 COUNT 列进行排序,我使用这种方法
- Doctrine2 order by count many to many
- Doctrine2, get entities ordered by a count of a many to many properties
但它给了我一个错误的回应:
- 它只显示我的任何加入实体的一行,因为它按图书 ID 分组
- 计数错误,因为它计算了所有连接行,所以数字通常非常大
发生这种情况是因为我的查询构建器中有太多左连接,如下所示:
$this->createQueryBuilder('b')
->addSelect('bookTranslations')
->addSelect('bookGenres')
->addSelect('bookCrews')
->addSelect('feels')
->addSelect('genre')
->addSelect('user')
->leftJoin('b.translations', 'bookTranslations')
->leftJoin('b.bookGenres', 'bookGenres')
->leftJoin('b.bookCrews', 'bookCrews')
->leftJoin('bookCrews.user', 'user')
->leftJoin('bookGenres.genre', 'genre')
->leftJoin('b.feels', 'feels')
->addSelect('COUNT(bookGenres) AS HIDDEN genreCount')
->groupBy('b.id')
;
我使用 symfony 序列化程序来处理响应数据
有什么想法吗?
最佳答案
由于->groupBy('b.id'),它应该按书显示一行,你希望书显示多少行?
至于排序,您缺少 orderBy :
->orderBy('genreCount', 'DESC')
关于php - Doctrine2 order by count many to many with many left join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66966624/