title: many to many relationship querybuilder doctrine and symfony2 extended query
body: <p>I am applying following query using querybuilder but somehow many2many relationship is not working as expected.</p>
  
  <pre><code>$shopData = $sm-&gt;createQueryBuilder()                    
                  -&gt;select('v')
                  -&gt;from('AdminBundle:Voucher','v')
                  -&gt;innerJoin('v.shop', 's')
                  -&gt;leftJoin('AdminBundle:VoucherProgram', 'vp', \Doctrine\ORM\Query\Expr\Join::ON, 'vp.id = v.program_id')
                  -&gt;leftJoin('AdminBundle:shopHistory', 'sh', \Doctrine\ORM\Query\Expr\Join::ON, 'sh.shop = s.id')
                  -&gt;where('s.shopStatus = :shopStatus')
                  -&gt;setParameter('shopStatus', Shop::SHOP_ACTIVATED)
                  -&gt;andWhere('s.highlightedHome = :highlightedHome')
                  -&gt;setParameter('highlightedHome', Shop::SHOP_HIGHLIGHTED_HOME)
                  -&gt;andWhere('s.offers = \'voucher\'')
                  -&gt;setFirstResult(0)
                  -&gt;setMaxResults(6)
                  -&gt;addOrderBy('v.discount_amount', 'DESC')
                  -&gt;groupBy('sh.shop')
                  -&gt;getQuery()
                  -&gt;getSql();
  </code></pre>
  
  <p>Generated query looks like below:</p>
  
  <pre><code>SELECT v FROM AdminBundle:Voucher v INNER JOIN v.shop s LEFT JOIN AdminBundle:VoucherPrograms vp ON vp.id = v.program_id LEFT JOIN AdminBundle:shopHistory sh ON sh.shop = s.id WHERE s.shopStatus = :shopStatus AND s.highlightedHome = :highlightedHome AND s.offers = 'voucher' GROUP BY sh.shop ORDER BY v.discount_amount DESC
  </code></pre>
  
  <p>In case if I remove everything and keep only inner join for Many2Many relation, it is working as expected.</p>
  
  <pre><code>$sm-&gt;createQueryBuilder()                    
                  -&gt;select('v')
                  -&gt;from('AdminBundle:Voucher','v')
                  -&gt;innerJoin('v.shop', 's');
  </code></pre>
  
  <p>Here is the generated query:</p>
  
  <pre><code>SELECT l0.* FROM voucher l0_ INNER JOIN shop_voucher l2_ ON l0_.id = l2_.voucher_id INNER JOIN shop l1_ ON l1_.id = l2_.shop_id;
  </code></pre>
  
  <p>So I am wondering why system is not picking up correct relation when I add more joins.</p>
  
  <p>Here is my main entities having Many2Many relationship:</p>
  
  <p>Shop.php</p>
  
  <pre><code>namespace AdminBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * Shop.
   *
   * @ORM\Table(name="shop")
   * @ORM\Entity(repositoryClass="AdminBundle\Entity\ShopRepository")
   */
  class Shop
  {
      const SHOP_DEACTIVATED = 0;
      const SHOP_ACTIVATED = 1;
      const SHOP_HIGHLIGHTED_HOME = 1;
      ................................
      /**
       * @ORM\ManyToMany(targetEntity="Voucher", inversedBy="shop")
       * @ORM\JoinTable(name="shop_voucher")
       */
      private $voucher;
      ................................
  </code></pre>
  
  <p>Voucher.php</p>
  
  <pre><code>namespace AdminBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  #use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * Voucher.
   *
   * @ORM\Table(name="voucher")
   * @ORM\Entity(repositoryClass="AdminBundle\Entity\VoucherRepository")
   */
  class Voucher
  {
      ................................
      /**
       * @ORM\ManyToMany(targetEntity="Shop", mappedBy="voucher", cascade={"persist"})    
       */
      private $shop;
      ................................
  </code></pre>
  
  <p>I have already checked on stack having same questions but I want to know how we can extend query. I have the solution for my question as below but not getting exactly what is happening in above scenario.</p>
  
  <pre><code>$shopDataQuery = $connection-&gt;prepare('SELECT v.* FROM voucher AS v INNER JOIN shop_voucher AS sv ON sv.voucher_id = v.id INNER JOIN shop AS s ON s.id = sv.shop_id LEFT JOIN voucher_programs AS vp ON vp.id = v.program_id LEFT JOIN shop_history AS sh ON sh.shop = s.id WHERE s.shopStatus = :shopStatus AND s.highlightedHome = :highlightedHome AND s.offers = 'voucher' GROUP BY sh.shop ORDER BY v.discount_amount DESC LIMIT 6');
  </code></pre>
  
  <hr>
  
  <p>Update:</p>
  
  <p>Here is the shopHistory.php</p>
  
  <pre><code>namespace AdminBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * shopHistory.
   *
   * @ORM\Table(name="shop_history")
   * @ORM\Entity(repositoryClass="AdminBundle\Entity\shopHistoryRepository")
   */
  class shopHistory
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var int
       * @ORM\ManyToOne(targetEntity="Shop", inversedBy="shopHistory")
       * @ORM\JoinColumn(name="shop", referencedColumnName="id")
       */
      private $shop;
  </code></pre>
  
  <p>Here is VoucherProgram.php</p>
  
  <pre><code>namespace AdminBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * VoucherProgram.
   *
   * @ORM\Table(name="voucher_program")
   * @ORM\Entity(repositoryClass="AdminBundle\Entity\VoucherProgramRepository")
   */
  class VoucherProgram
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\OneToMany(targetEntity="Voucher", mappedBy="program")
       */
      private $voucher;
  
      /**
       * @ORM\OneToMany(targetEntity="Shop", mappedBy="vprogram")
       */
      private $shop;
  </code></pre>