Doctrine2 Multiple Join works with createQuery, but not with queryBuilder

If I use a query 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 (what I believe is the same), a query through a 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:

"Semantic error" line 0, col 66 next to .colours c, LikeYeah \ GoBundle \ Entity \ Product ': Error: identification variable g used in the connection path expression, but has not been defined previously. "

What am I missing?

+6
source share
5 answers

Try the following: using addSelect after combining:

  $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'); 
+3
source

You can help with this method

  public function findSampleClothingTypeGender($gender) { $query = $this->getEntityManager() ->createQuery(' SELECT p FROM Acme:Product p JOIN p.clothing_type ct WHERE p.gender = :gender' )->setParameter('gender', $gender); try { return $query->getResult(); } catch (\Doctrine\ORM\NoResultException $e) { return null; } } 
0
source

Try with below one.

  $qb->select('p','g','c') ->from(array('LikeYeah\GoBundle\Entity\Product','p')) ->join(array('p.garments','g')) ->join(array('g.colours','c'),'LEFT') ->order_by('p.id','DESC'); 
0
source

You can also try

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

show result of $ qb init and DQL

-1
source

This works for me.

 $this->_em->createQueryBuilder() ->select('fu,e,t') ->from('\xxxx\AdminBundle\Entity\FrontUser','fu') ->join('fu.read_essays','e') ->leftJoin('e.tags','t') ->getQuery()->execute(); 

I think you need to create a new QueryBuilder object.

You can use the following code to see the Dql of this QueryBuilder

$ qb-> getDQL ();

-1
source

Source: https://habr.com/ru/post/907660/


All Articles