doctrine 2查询构建器和连接表 [英] doctrine 2 query builder and join tables

查看:288
本文介绍了doctrine 2查询构建器和连接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

但是我收到这个错误

  [语义错误]行0,col 58附近的'.comments c'':错误:
标识变量a用于连接路径表达式,但之前未定义。

PS:映射是正确的,我可以看到页面文章与其注释

解决方案

如果这仍然给您问题,这里是使用Doctrine 2.1文档中的示例中发现的语法查询。



我假设您的查询位于自定义存储库方法中,a是文章的缩写。

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

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

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

return $ results;


hello guys im tryin 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 im gettin 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 cus i can see the page article with its comments

解决方案

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;

这篇关于doctrine 2查询构建器和连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆