title: zf2 + doctrine architecture. How to use object manager?
body: <p>What is the best way to use Doctrine ObjectManager? I inject it into controller at module.config.php </p>
  
  <pre><code>'Telecom\Controller\Users' =&gt; function($sm){
      $ctr = new Telecom\Controller\UsersController();
      $ctr-&gt;setEntityManager(
                 $sm-&gt;getServiceLocator()
                      -&gt;get('Doctrine\ORM\EntityManager')
             );
  
      return $ctr;
  },
  </code></pre>
  
  <p>Then I use it in my controller as follows</p>
  
  <pre><code>$this-&gt;getEntityManager()-&gt;persist($entity);
  $this-&gt;getEntityManager()-&gt;flush();
  </code></pre>
  
  <p>But Marco Pivetta (Doctrine team, zf2 contributor) teaches "If you inject objectmanager in your controllers, you're gonna have a bad architecture" <a href="http://marco-pivetta.com/doctrine-orm-zf2-tutorial/#/39/11" rel="nofollow">http://marco-pivetta.com/doctrine-orm-zf2-tutorial/#/39/11</a>.</p>
  
  <p>So please help me, what is the best architecture way to use Entity Manager. Should I use another layer like my own service to deal with entity manager?</p>