主义加入绕过懒惰加载 [英] Doctrine join bypass lazy loading
问题描述
我刚刚开始探索Symfony2,令我感到惊奇的是它有多少优秀功能。我开始做博客教程:
http://tutorial.symblog.co.uk/
但使用版本2.1而不是2.0
I just started exploring Symfony2 and I am amazed how many great features it has. I started doing the blog tutorial at:
http://tutorial.symblog.co.uk/
but using version 2.1 instead of 2.0
我的问题是我有以下博客实体:
My problem is that i have the following entity for Blog:
/**
* @ORM\OneToMany(targetEntity="Comment", mappedBy="blog")
*/
protected $comments;
以及注释实体中的以下内容:
and the following in the Comment entity:
/**
* @var string $blog
*
* @ORM\ManyToOne(targetEntity="Blog", inversedBy="comments")
* @ORM\JoinColumn(name="blog_id", referencedColumnName="id")
*/
private $blog;
我创建了获取最新博客并加入评论的功能,所以我不必懒惰加载评论对于每个博客,并避免对这些注释表进行多次调用:
I created the function to get the latest blogs and join the comments so i dont have to lazy load comments for each blog and avoid multiple calls to the comments table like so:
$qb = $this->createQueryBuilder('b')
->select('b')
->leftJoin('b.comments', 'c')
->addOrderBy('b.created', 'DESC');
但是当我在树枝模板中运行时,该查询的结果如下: blog.comments
我得到第一个查询来检索博客是正确的加入它,但然后,而不是使用联结的价值评论我可以调用每个博客条目的注释表?如何避免这种情况的功能,但是保持关系,以便我可以从视图博客页面获取博客的所有评论。
but when i run in the twig template the result from that query as: blog.comments
I got the first query to retrieve the blogs correct with the join in it but then instead of using the joined value for comments i get a call to the comments table for each blog entry? How do I bypass that functionality for that certain case but keep the relationship so i can get all comments for a blog from the view blog page?
推荐答案
您还需要将注释对象添加到选择结果中:
You also need to add the comment objects to the select results:
->select('b', 'c')
这篇关于主义加入绕过懒惰加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!