Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 Multiple Join works with createQuery but not with queryBuilder

If I'm using querying without queryBuilder with this dql

$query = $this->_em
  ->createQuery("SELECT p, g, c
            FROM LikeYeah\GoBundle\Entity\Product p
            JOIN p.garments g
            LEFT JOIN g.colours c
            ORDER BY p.id DESC
          ");

everything is fine, but if I use the (what I belive is the same) query trough query builder like this

 $qb->select('p, g, c')
    ->from('LikeYeah\GoBundle\Entity\Product', 'p')
    ->join('p.garments', 'g')
    ->leftJoin('g.colours', 'c')
    ->orderBy('p.id', 'desc');

I get the following error:

"Semantical Error] line 0, col 66 near '.colours c, LikeYeah\GoBundle\Entity\Product': Error: Identification Variable g used in join path expression but was not defined before."

What am I missing?

like image 936
Marko Jovanović Avatar asked Feb 04 '12 19:02

Marko Jovanović


1 Answers

Try this: using addSelect after your joins:

 $qb->select('p')
    ->join('p.garments', 'g')
    ->addSelect('g')
    ->from('LikeYeah\GoBundle\Entity\Product', 'p')
    ->join('p.garments', 'g')
    ->leftJoin('g.colours', 'c')
    ->addSelect('c')
    ->orderBy('p.id', 'desc');
like image 134
Lighthart Avatar answered Sep 23 '22 22:09

Lighthart