Search code examples
ormdoctrine-ormdql

Doctrine 2 DQL query with class name?


Is it possible in DQL, to specify the class name as a WHERE or GROUP BY clause?

That would be very useful when having a hierarchy of users, for example:

SELECT a FROM Article a
JOIN a.owner o
WHERE o.class = ?
SELECT u.class, COUNT(*)
FROM User u
GROUP BY u.class

Solution

  • There's the "INSTANCE OF" syntax in DQL:

    your first example:

    SELECT a FROM Article a
    JOIN a.owner o
    WHERE o INSTANCE OF 'Editor'
    

    I'm not sure you do what you're trying to do in the second case, though.