Tibo
Tibo

Reputation: 197

Symfony/Doctrine2 and Associative entity

Usually when querying in a custom repository class, I use something like this :

SELECT * FROM BundleName:Entity

But how do I do for associative entity ?

I have an entity "Ticket" and an entity "Tag". It's a ManyToMany relation.

In phpMyAdmin, I've got a ticket_tag associative table but how do I get it with Doctrine ?

Thank you

Upvotes: 1

Views: 252

Answers (1)

Ashutosh Rai
Ashutosh Rai

Reputation: 457

You should use createQueryBuilder to handle your custom query requirement, in case if you are having a valid relationship over entities. for example: Inside ticket repository you should handle like this, if you want to do more operations then you should learn more from here: https://symfony.com/doc/3.3/doctrine/repository.html

$query = $this->createQueryBuilder('t')
     ->select('count(t.id) as total_ticket, tag.id as tagId')
     ->leftJoin('t.tags', 'tag')
     ->groupBy('tag.id')
;

return $query->getQuery()->getResult();

Upvotes: 4

Related Questions