title: Doctrine2 , get entities order by count of a many to many property
body: <p>I have two entities </p>
  
  <ul>
  <li>Article</li>
  <li>User</li>
  </ul>
  
  <p>Article has a relation to user named 'likedByUsers'</p>
  
  <p>Now, I would like to get the articles order by number of likes, but:</p>
  
  <ul>
  <li>I don't want to have a property 'numberOfLikes' because it's too much problem to keep it updated</li>
  <li>I have too many Articles (100k+) to be realistic to the "sort" in PHP side (and the fact we're reaching the limit of doing the sort is the reason why I'm asking this question) </li>
  <li>I can live with not getting the number of likes in the returned values (as the serializer will later hydrate it)</li>
  </ul>
  
  <p>what I currently have is this: </p>
  
  <pre><code>$builder = $this-&gt;createQueryBuilder('a');
      -&gt;select('COUNT(u) AS nbrLikes')
      -&gt;leftJoin('a.likedByUsers', 'u') 
      -&gt;orderBy('nbrLikes', 'DESC')
      -&gt;groupBy('a.id')
      -&gt;getQuery()
      -&gt;getResult()
  ;
  </code></pre>
  
  <p>this correctly returns the number of likes (with 0 for articles without likes), but it does NOT return the Articles themselves </p>
  
  <p>I've tried adding </p>
  
  <pre><code>-&gt;select('a, COUNT(u) AS HIDDEN nbrLikes')
  </code></pre>
  
  <p>but it fails because <code>a</code> is not part of the <code>GROUP BY</code></p>
  
  <p>any ideas ?</p>