I have two entities (Item & Tag) linked by a bidirectional ManyToMany relation, and I'd like to display the entity (Tag) records that are actually used in a relation with the other entity (Item) :
Here is my Item entity:
class Item
{
/**
* @ORM\ManyToMany(targetEntity="MyBundle\Entity\Tag", inversedBy="items")
*/
private $tags;
}
And my Tag enity :
class Tag
{
/**
* @ORM\ManyToMany(targetEntity="MyBundle\Entity\Item", mappedBy="tags")
*/
private $items;
}
Now in my Tag Repository I've tried this :
class TagRepository extends \Doctrine\ORM\EntityRepository
{
public function findAllUsed()
{
return $this->createQueryBuilder('t')
->leftJoin('t.items', 'items')
->groupBy('items.id')
->having('COUNT(t.id) > 0')
->orderBy('t.name', 'ASC')
->getQuery()
->getResult();
}
}
But it doesn't give me the result I'm expecting... Can anyone help? Thanks!