title: DQL query with joining table and two join
body: <p>I have 2 entity:</p>
  
  <pre><code>/**
   * @ORM\Entity
   * @ORM\Table(name="users")
   */
  class User
  {
      /**
       * @ORM\ManyToMany(targetEntity="Myapp\UserBundle\Entity\Group")
       * @ORM\JoinTable(name="user_groups",
       *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
       * )
       */
      protected $groups;
  
      ...
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>/**
   * @ORM\Entity(repositoryClass="Myapp\UserBundle\Repository\GroupRepository")
   * @ORM\Table(name="groups")
   */
  class Group
    ...
  </code></pre>
  
  <p>I cant find a way to create a DQL query which results SQL like this:</p>
  
  <pre><code>SELECT g.name, g.id, count( u.id )
  FROM users u
  LEFT JOIN user_groups ug ON u.id = ug.user_id
  RIGHT JOIN groups g ON g.id = ug.group_id
  GROUP BY g.id
  </code></pre>
  
  <p>I tried and failed whith:</p>
  
  <pre><code>$this-&gt;getEntityManager()
      -&gt;createQuery('
          SELECT g.id, g.name, count(u.id) as usercount FROM MyappUserBundle:User u
          JOIN u.groups g
          GROUP BY g.id'
      );
  </code></pre>
  
  <p>since the result not contains the groups that has no user.</p>