I have the following DQL statement, using Doctrine's querybuilder:
As you can see I am bringing back all the posts and their comments:
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')
I have already tried adding a
->addSelect("COUNT(p.comments) as countComments")
And I just get an error, "countComments' does not point to a Class"
So I looked up other references, like this one: https://symfonycasts.com/screencast/doctrine-queries/select-sum
But it doesn't give an example of how to include a count in with the results of a DQL query.
Do I need to just create my own count function within the comments repository, and loop through my original data set, calling it once per post?