doctrine 2 query builder and join tables

2019-01-24 01:16发布

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.

1条回答
forever°为你锁心
2楼-- · 2019-01-24 01:37

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;
查看更多
登录 后发表回答