title: Doctrine does not automatically load joined relation using GROUP BY
body: <p>I have two Doctrine entity classes: <code>Vertriebsschiene</code> and <code>Filiale</code>:</p>
  
  <pre><code>/**
   * Vertriebsschiene
   *
   * @ORM\Table(name="vertriebsschiene", indexes={
   *     @ORM\Index(columns={"name"}, flags={"fulltext"})
   * }))
   * @ORM\Entity(repositoryClass="CRMBundle\Repository\VertriebsschieneRepository")
   */
  class Vertriebsschiene
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
  
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=100)
       */
      private $name;
  
  
  
      /**
       * @var ArrayCollection
       *
       * @ORM\OneToMany(targetEntity="Filiale", mappedBy="vertriebsschiene", fetch="EAGER")
       */
      private $filialen;
  
      ...
  }
  </code></pre>
  
  <hr>
  
  <pre><code>/**
   * Filiale
   *
   * @ORM\Table(name="filiale")
   * @ORM\Entity(repositoryClass="CRMBundle\Repository\FilialeRepository")
   */
  class Filiale extends Lieferant
  {
      /**
       * @var Vertriebsschiene
       *
       * @ORM\ManyToOne(targetEntity="CRMBundle\Entity\Vertriebsschiene", inversedBy="filialen", fetch="EAGER")
       */
      private $vertriebsschiene;
  
      ...
  }
  </code></pre>
  
  <p>The <code>Vertriebsschine</code> objects have a non-uniqe name. Now I try to display a list of <code>Vertriebsschiene</code> objects with their <code>Filiale</code> objects. </p>
  
  <p>My findAllQuery method looks like this:</p>
  
  <pre><code>/**
   * @param User $user
   * @return \Doctrine\ORM\Query
   */
  public function findAllQuery(User $user){
  
      $qb = $this-&gt;createQueryBuilder('v')
          -&gt;select('v as vertriebsschiene')
          -&gt;addSelect('COUNT(f) as filial_num')
          -&gt;leftJoin('v.filialen', 'f')
          -&gt;groupBy('v.name');
  
      $this-&gt;restrictAccess($user, $qb);
  
      return $qb-&gt;getQuery();
  }
  
  
  /**
   * @param User $user
   * @param $qb
   */
  protected function restrictAccess(User $user, QueryBuilder &amp;$qb)
  {
      if ($user-&gt;hasRole(RoleVoter::AUSSENDIENST)) {
  
          $qb -&gt;leftJoin('f.vertreter', 'u')
              -&gt;leftJoin('u.vertretungen', 'vx')
              -&gt;andWhere($qb-&gt;expr()-&gt;orX(
                  'f.vertreter = :userid',
                  'f.vertreter IS NULL',
                  $qb-&gt;expr()-&gt;andX(
                      'vx.proxy = :userid',
                      $qb-&gt;expr()-&gt;between(':currentDate', 'vx.start', 'vx.end')
                  )
              ))
              -&gt;setParameter('userid', $user-&gt;getId())
              -&gt;setParameter('currentDate', new \DateTime(), \Doctrine\DBAL\Types\Type::DATETIME);
      }
  }
  </code></pre>
  
  <p>My problem is, that the <code>Vertriebsschiene::$filiale</code> array collection is not automatically loaded, but is loaded for every <code>Vertriebsschiene</code> resulting in many DB connections. </p>
  
  <p>This also has the problem, that the <code>WHERE</code> statement is ignored when the <code>Vertriebsschiene::$filiale</code> is fetched.</p>
  
  <p>The <code>COUNT(f)</code> returns the correct amount of <code>Filiale</code> objects.</p>
  
  <p>I suspect this is an issue with the <code>GROUP BY</code> statement.</p>