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.

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

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)