How to call Entity Manager in a constructor?

前端 未结 6 1541
终归单人心
终归单人心 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:27

    Don't extend the base controller class when you register controller as a service. There is a documentation about it here

    class ImageTestController
    {
         private $em;
    
         public function __construct(EntityManager $em)
         {
             $this->em = $em;
         }
    
         public function someAction()
         {
             // do something with $this->em
         }
    }
    
    // services.yml
    services:
        acme.controller.image_test:
            class: Acme\SomeBundle\Controller\ImageTestController
    
    // routing.yml
    acme:
        path: /
        defaults: { _controller: acme.controller.image_test:someAction }
    

提交回复
热议问题