学说 2 查询生成器和连接表 [英] doctrine 2 query builder and join tables

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

问题描述

我试图在我的主页上获得每个帖子的所有评论

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

return 
$this->createQueryBuilder('c')
->select('c')
->from('SdzBlogBundleEntityCommentaire' ,'c')                
->leftJoin('a.comments' ,'c')->getQuery()->getResult() ;

但我收到此错误

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

PS:映射是正确的,因为我可以看到带有评论的页面文章.

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

推荐答案

如果这仍然给您带来问题,请使用 Doctrine 2.1 文档中的示例中的语法进行查询.

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.

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

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('SdzBlogBundleEntityArticle', 'a')
   ->leftJoin('a.comments', 'c');

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

return $results;

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

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