How to call Entity Manager in a constructor?

前端 未结 6 1556
终归单人心
终归单人心 2020-12-06 05:52

I\'ve been trying to call Entity Manager in a constructor:

function __construct()
{
    $this->getDoctrine()->getEntityManager();
    ...
         


        
6条回答
  •  执念已碎
    2020-12-06 06:31

    I see that you are trying to get the entity manager in the constructor of the controller, which is not the way to do so , unless you plan to define your controller as a service. which on this case, you need to use dependency injection to inject the service entity manager.

    But in general the common way to use entity manager in a controller is simply by getting it using the following code:

      $entityManager = $this->container->get('doctrine.orm.entity_manager');
    

提交回复
热议问题