title: How to avoid a lot of queries when counting associated entities with Doctrine?
body: <p>Imagine an <code>User</code> entity with an association with <code>Group</code> (many-to-many). I need to display user details (along with - say - group names or groups count). In my controller (Symfony 2) I can get all users:</p>
  
  <pre class="lang-php prettyprint-override"><code>$users = $this-&gt;em-&gt;getRepository('My\Entity\User')
      -&gt;getAll();
  
  // Assign $users to whatever the view engine
  return $this-&gt;render('users.html.twig', array('users' =&gt; $users));
  </code></pre>
  
  <p>Of course , being the association LAZY by default, no JOIN operations with <code>Group</code> are performed. This is good most of the time.</p>
  
  <p>But what happens when I try to get group count in my view?</p>
  
  <pre class="lang-none prettyprint-override"><code>{% for user in users %}
      {{ user.groups|length }}
  {% endfor %}
  </code></pre>
  
  <p>The result is: one query performed for each raw. That is, the number of the query equals the number of the user. What happens with 1000 users (and no pagination)?</p>
  
  <p>How can fetch all associated entity for the <code>User</code> class (i.e. issue just one query)?</p>