添加doctrine2查询的所有相关项的计数

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->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")
);