doctrine 2 query builder and join tables
Asked Answered
B

1

19

I'm trying to get all comments for each post in my home page

return 
$this->createQueryBuilder('c')
->select('c')
->from('Sdz\BlogBundle\Entity\Commentaire' ,'c')                
->leftJoin('a.comments' ,'c')->getQuery()->getResult() ;

but I'm getting this error

[Semantical Error] line 0, col 58 near '.comments c,': Error:
Identification Variable a used in join path expression but was not defined before.

PS : The mapping is correct because I can see the page article with its comments.

Boyne answered 6/11, 2011 at 23:48 Comment(2)
i found out how guys here return $this->createQueryBuilder('a') ->select('a ,c') ->leftJoin('a.comments' ,'c') ->getQuery() ->getResult() ;Boyne
can you pls check #17115665 I tried you solution but its not working fo me..Strawser
A
38

In case this is still giving you problems, here is your query using the syntax found in the examples in the Doctrine 2.1 documentation.

I'm assuming your query resides in a custom repository method, and that 'a' is an abbreviation for 'Article'.

$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();

$qb->select(array('a', 'c'))
   ->from('Sdz\BlogBundle\Entity\Article', 'a')
   ->leftJoin('a.comments', 'c');

$query = $qb->getQuery();
$results = $query->getResult();

return $results;
Azilian answered 7/11, 2011 at 1:36 Comment(1)
with reference to this question i am trying to solve my problem but i am facing issues. #17115665Strawser

© 2022 - 2024 — McMap. All rights reserved.