Doctrine2: Many-to-Many select all entries, with/without relations

时间:2018-06-18 11:41:16

标签: doctrine-orm many-to-many

I have a many to many bidirectional relation in my app, right like in official guide.

Using a relational model User <-> Group from the link above - lets say I want to select all entries from Group entity with the following condition

1) Select all groups that have at least one user related to a group.
2) Select all groups that have no users in it.

I can't figure out how to prepare a correct DQL, any ideas please.

1 个答案:

答案 0 :(得分:0)

在纯DQL中,您可以编写为

SELECT g, 
       COUNT(u.id) AS total_users
FROM Entity\Group g
LEFT JOIN g.users u
GROUP BY g.id
HAVING total_users >= 0

Doctrine2 get object without relations