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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
kosaidpo
  • 461
  • 1
  • 5
  • 14
  • 1
    i found out how guys here `return $this->createQueryBuilder('a') ->select('a ,c') ->leftJoin('a.comments' ,'c') ->getQuery() ->getResult() ;` – kosaidpo Nov 07 '11 at 00:14
  • can you pls check http://stackoverflow.com/questions/17115165/doctrine-inner-left-join-two-tables I tried you solution but its not working fo me.. – DonOfDen Jun 14 '13 at 19:05

1 Answers1

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;
Ven
  • 19,015
  • 2
  • 41
  • 61
cantera
  • 24,479
  • 25
  • 95
  • 138
  • with reference to this question i am trying to solve my problem but i am facing issues. http://stackoverflow.com/questions/17115165/doctrine-inner-left-join-two-tables – DonOfDen Jun 14 '13 at 19:01