doctrine 2 query builder and join tables

69,793

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;
Share:
69,793
kosaidpo
Author by

kosaidpo

Updated on July 05, 2022

Comments

  • kosaidpo
    kosaidpo almost 2 years

    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.

  • DonOfDen
    DonOfDen about 11 years
    with reference to this question i am trying to solve my problem but i am facing issues. stackoverflow.com/questions/17115165/…