gpt4 book ai didi

join - Doctrine join 绕过延迟加载

转载 作者:行者123 更新时间:2023-12-05 00:38:19 31 4
gpt4 key购买 nike

我刚刚开始探索 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评论的值(value) 我会为每个博客条目调用评论表吗?我如何在特定情况下绕过该功能但保持关系以便我可以从查看博客页面获取博客的所有评论?

最佳答案

您还需要将评论对象添加到选择结果中:

->select('b', 'c')

关于join - Doctrine join 绕过延迟加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11659145/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com