Search code examples
phpsqldoctrine-ormdql

Order a DQL Join by count on assiociation


I have a DQL query likes this:

$dql = "SELECT p, count(u.id) as ucnt FROM IAA\Entity\Project p " .
       "JOIN p.likedByUsers u ORDER BY ucnt";
$query = $this->_em->createQuery($dql);

Question is, how can I hold on to sorting by count, but NOT get them in result? I want to get only projects sorted by count, not array with [project] and [ucnt].


Solution

  • Starting from Doctrine 2.2 (source) :

    SELECT a, COUNT(b.id) AS HIDDEN c [...] ORDER BY c