title: Doctrine2 Lazy Loading
body: <p>I was reading about lazy associations in Doctrine 2 and how I could avoid the following situation:</p>
  
  <p><img src="https://i.stack.imgur.com/u7gM5.png" alt="Entity dump in twig template"></p>
  
  <p>In this <a href="http://doctrine-orm.readthedocs.org/en/latest/tutorials/extra-lazy-associations.html#enabling-extra-lazy-associations" rel="nofollow noreferrer">paragraph</a> 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.</p>
  
  <p>So far I tried some adjustments to the entity repository but without any success. I also tried <a href="https://stackoverflow.com/questions/9735930/how-to-prevent-doctrine-from-lazy-loading-one-to-one-relationsip">this post</a>, <a href="https://stackoverflow.com/questions/14431869/lazy-loading-with-doctrine-one-to-one-mapping?rq=1">this post</a> and <a href="https://stackoverflow.com/questions/20928247/symfony-2-doctrine-2-one-to-many-relationship-not-returning-data?rq=1">this post</a> but they seem to handle ManyToMany or a complete other situation.</p>
  
  <p>Could somebody explain how and where to use lazy association to avoid the above example?</p>
  
  <p>Irrelevant private properties and getters/setters have been removed from this code snippets due to the length.</p>
  
  <p><kbd>src/AppBundle/Entity/News.php</kbd></p>
  
  <pre class="lang-php prettyprint-override"><code>class News
  {
      /**
       * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Account", fetch="EXTRA_LAZY")
       * @ORM\JoinColumn(name="author", referencedColumnName="id")
       */
      private $author;
  }
  </code></pre>
  
  <p><kbd>src/AppBundle/Entity/Repositories/NewsRepository.php</kbd></p>
  
  <pre class="lang-php prettyprint-override"><code>class NewsRepository extends EntityRepository
  {
      /**
       * @param $id
       * @return mixed
       * @throws \Doctrine\ORM\NonUniqueResultException
       */
      public function findOneById($id) {
          return $this-&gt;createQueryBuilder('a')
                      -&gt;andWhere('a.id = :id')
                      -&gt;setParameter('id', $id)
                      -&gt;getQuery()
                      -&gt;getOneOrNullResult();
      }
  }
  </code></pre>
  
  <p><kbd>src/AppBundle/Controller/NewsController.php</kbd></p>
  
  <pre class="lang-php prettyprint-override"><code>/**
   * @Route("/{id}", name="news_item")
   * @Method({"GET"})
   * @Template("AppBundle:news:item.html.twig")
   */
  public function articleAction(Request $request, $id)
  {
      $news_item = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle:News')-&gt;findOneById($id);
  
      if (!$news_item) {
          throw $this-&gt;createNotFoundException('No news item found by id ' . $id);
      }
  
      return array(
          'news_item' =&gt; $news_item
      );
  }
  </code></pre>
  
  <p><kbd>src/AppBundle/Resources/views/news/item.html.twig</kbd></p>
  
  <pre class="lang-twig prettyprint-override"><code>{% extends 'base.html.twig' %}
  
  {% block body %}
      {{ dump(news_item) }} }}
  {% endblock %}
  </code></pre>