0

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.

Majesty
  • 2,097
  • 5
  • 24
  • 55

1 Answers1

0

In pure DQL you could write is as

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

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118