Adding a count of all the related items for a doctrine2 query
I've this Query on doctrine2 that basically returns the different tags a Host have. May not be the best way to do it but it works. See, the tag<->ticket<->host are both manytomany relations.
$qb-开发者_StackOverflow>select('t')
->from('App\Entity\Tag', 't')
->join('t.tickets', 'p')
->join('p.hosts', 'b')
->where('b.id = '. $this->host->getId())
->add('orderBy', 't.name ASC');
As i said the problem is not this query (that works!), but i would like to add a count there to see how many tickets the returned Tag has. Been tryin all day with:
$qb->expr()->countDistinct("p.id");
Or even with DQL but can't make it work, would appreciate any suggestion.
Regards,
You could try:
$qb->addSelect(
$qb->expr()->countDistinct("p.id")
);
精彩评论