我刚开始探索 Symfony2,我很惊讶它有这么多很棒的功能。我开始在http://tutorial.symblog.co.uk/上写博客教程,
但使用的是 2.1 版而不是 2.0 版
我的问题是我有以下博客实体:
/**
* @ORM\OneToMany(targetEntity="Comment", mappedBy="blog")
*/
protected $comments;
以及 Comment 实体中的以下内容:
/**
* @var string $blog
*
* @ORM\ManyToOne(targetEntity="Blog", inversedBy="comments")
* @ORM\JoinColumn(name="blog_id", referencedColumnName="id")
*/
private $blog;
我创建了获取最新博客并加入评论的功能,因此我不必为每个博客延迟加载评论并避免多次调用评论表,如下所示:
$qb = $this->createQueryBuilder('b')
->select('b')
->leftJoin('b.comments', 'c')
->addOrderBy('b.created', 'DESC');
但是当我在 twig 模板中运行时,该查询的结果为:blog.comments
我得到了第一个查询来检索正确的博客,并在其中加入,但随后我没有使用连接值进行评论,而是调用了每个评论表博客条目?对于这种情况,我如何绕过该功能但保持关系,以便我可以从查看博客页面获取博客的所有评论?