title: Semantical Error - Doctrine 2.0
body: <p>These are my tables:</p>
  
  <pre><code>    Table Gift:
          -id 
          -price
          ... 
  </code></pre>
  
  <p><br></p>
  
  <pre><code>    Table Couple:
           -id
           -name 
          ... 
  </code></pre>
  
  <p><br></p>
  
  <pre><code>    table registry:  //provide a many-many relation between gifts and couples 
           -id 
           -coupleId 
           -giftId 
  </code></pre>
  
  <p><br></p>
  
  <pre><code>    table purchase:
           -amount
           -registryId 
  </code></pre>
  
  <p><br></p>
  
  <p>I already wrote a sql query to get all the gift info for a specific couple<br>
  <br></p>
  
  <pre><code>    $qb = $this-&gt;createQueryBuilder('g') //gift
      -&gt;from('\BBB\GiftBundle\Entity\Registry', 'reg')
      -&gt;select('g.id , g.price')
      -&gt;where('reg.gift = g.id')
      -&gt;andWhere('reg.couple = :coupleID')
      -&gt;orderBy('reg.id','DESC')
      -&gt;setParameter('coupleID', $coupleID);
  </code></pre>
  
  <p><br> OR <br></p>
  
  <pre><code>     SELECT g.id , g.price,
      FROM gift g, registry reg       
      WHERE reg.gift_id = g.id AND reg.couple_id = 1 
  </code></pre>
  
  <p><br><br>
  I also want to get the total amount that for the gifts that have been bought (if any) <br>
  EX. SUM(purchase.amount) as totalContribute  <br>
  <br>
  I have tried:</p>
  
  <pre><code>    $qb = $this-&gt;createQueryBuilder('g')
      -&gt;from('\BBB\GiftBundle\Entity\Purchase', 'p')
      -&gt;from('\BBB\GiftBundle\Entity\Registry', 'reg')
      -&gt;select('g.id ,  g.price')
      -&gt;addSelect('SUM(p.amount) as totalContribute')
      -&gt;leftJoin('p','pp', 'ON','reg.id = pp.registry')
      -&gt;where('reg.gift = g.id')
      -&gt;andWhere('reg.couple = :coupleID')
      -&gt;orderBy('reg.id','DESC')
      -&gt;setParameter('coupleID', $coupleID);
  </code></pre>
  
  <p>but it gives me the following error:<br></p>
  
  <pre><code>    [Semantical Error] line 0, col 145 near 'pp ON reg.id': Error: Identification Variable p used in join path expression but was not defined before.
  </code></pre>