douguanya4248 2015-06-23 19:34
浏览 72
已采纳

Doctrine2懒惰加载

I was reading about lazy associations in Doctrine 2 and how I could avoid the following situation:

Entity dump in twig template

In this paragraph in the documentation is explained how to enable lazy associations for your entity. I am missing how I could use this within my entity repository.

So far I tried some adjustments to the entity repository but without any success. I also tried this post, this post and this post but they seem to handle ManyToMany or a complete other situation.

Could somebody explain how and where to use lazy association to avoid the above example?

Irrelevant private properties and getters/setters have been removed from this code snippets due to the length.

<kbd>src/AppBundle/Entity/News.php</kbd>

class News
{
    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Account", fetch="EXTRA_LAZY")
     * @ORM\JoinColumn(name="author", referencedColumnName="id")
     */
    private $author;
}

<kbd>src/AppBundle/Entity/Repositories/NewsRepository.php</kbd>

class NewsRepository extends EntityRepository
{
    /**
     * @param $id
     * @return mixed
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function findOneById($id) {
        return $this->createQueryBuilder('a')
                    ->andWhere('a.id = :id')
                    ->setParameter('id', $id)
                    ->getQuery()
                    ->getOneOrNullResult();
    }
}

<kbd>src/AppBundle/Controller/NewsController.php</kbd>

/**
 * @Route("/{id}", name="news_item")
 * @Method({"GET"})
 * @Template("AppBundle:news:item.html.twig")
 */
public function articleAction(Request $request, $id)
{
    $news_item = $this->getDoctrine()->getRepository('AppBundle:News')->findOneById($id);

    if (!$news_item) {
        throw $this->createNotFoundException('No news item found by id ' . $id);
    }

    return array(
        'news_item' => $news_item
    );
}

<kbd>src/AppBundle/Resources/views/news/item.html.twig</kbd>

{% extends 'base.html.twig' %}

{% block body %}
    {{ dump(news_item) }} }}
{% endblock %}
  • 写回答

1条回答 默认 最新

  • dongyue7796 2015-06-23 20:31
    关注

    You don't have to do anything special to enable lazy loading. Extra lazy loading in the relationship you show isn't necessary for News to not load an Author. It just means you can make calls like ->contains on a collection without loading the entire collection & a few other conveniences.

    dump() should show something on Author like:

    protected 'author' => 
        object(Proxies\__CG__\YourNamespace\YourBundle\Entity\Author)
    

    This doesn't mean the entity has been fetched from the db. To quote the docs.

    Instead of passing you back a real Author instance and a collection of comments Doctrine will create proxy instances for you. Only if you access these proxies for the first time they will go through the EntityManager and load their state from the database.

    If you don't get back a proxy class, it's probably because you've already accessed that relationship earlier in your code. Or you've explicitly fetched that entity in your query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作