I am trying to create a query to retrieve objects from a Doctrine database, sorted by the number of members of a specific one-to-many relationship.
In particular: I have two entities: a person and a federation. A person can be a member of one federation (a person has a "federation" relationship), and a federation can have Russian people (a federation as a "people" relationship).
I would like to create a DQL query that returns a list of federations ordered by how many people are members of this Federation. Something like that:
SELECT f FROM AcmeStatsBundle:Federation f ORDER BY [number of members of f.people]
This will be the first step. There is an additional second step, which I do not know whether it is possible to achieve with a single query, which will filter the members of the relationship before counting. For instance:
SELECT f FROM AcmeStatsBundle:Federation f ORDER BY [number of (f.people p where p.attr = value)]
This second would be the optimal result, but the first satisfies my needs if the second case is not possible in one request.
Thanks in advance.
source
share