【发布时间】:2019-05-30 17:07:39
【问题描述】:
我有以下 DQL 语句,使用 Doctrine 的查询构建器:
如您所见,我将带回所有帖子及其 cmets:
public function getAllPosts(User $user){
$qb = $this->createQueryBuilder('p');
$qb->select('p, postPhotos,postVideos, comments, commentUser, commentUserPhoto, replyComments, commentReplyUser, commentReplyUserPhoto, postTier,creator,creatorPhoto,creatorTiers,creatorSubscriptions')
->leftJoin('p.photos', 'postPhotos')
->leftJoin('p.videos', 'postVideos')
->leftJoin('p.comments', 'comments')
->leftJoin('comments.user', 'commentUser')
->leftJoin('commentUser.photos', 'commentUserPhoto', 'WITH', 'commentUserPhoto.type = :profileType')
->leftJoin('comments.children', 'replyComments')
我已经尝试添加一个
->addSelect("COUNT(p.comments) as countComments")
我只是得到一个错误,“countComments' 不指向一个类”
所以我查找了其他参考资料,例如:https://symfonycasts.com/screencast/doctrine-queries/select-sum
但它没有给出如何在 DQL 查询结果中包含计数的示例。
我是否只需要在 cmets 存储库中创建自己的计数函数,并遍历我的原始数据集,每个帖子调用一次?
【问题讨论】: