How to call Entity Manager in a constructor?

前端 未结 6 1467
终归单人心
终归单人心 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 }
    
    0 讨论(0)
  • 2020-12-06 06:27

    You have to add

    use Doctrine\ORM\EntityManager;
    

    in your controller

    0 讨论(0)
  • 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');
    
    0 讨论(0)
  • 2020-12-06 06:40

    You need to make a service for your class and pass the doctrine entity manager as the argument doctrine.orm.entity_manager.Like in services.yml

    services:
      test.cutom.service:
        class:  Test\YourBundleName\Yourfoldernameinbundle\Test
        #arguments:
        arguments: [ @doctrine.orm.entity_manager  ] 
            #entityManager: "@doctrine.orm.entity_manager"
    

    You must import your services.yml in config.yml

    imports:
        - { resource: "@TestYourBundleName/Resources/config/services.yml" }
    

    Then in your class's constructor get entity manager as argument

    use Doctrine\ORM\EntityManager;
    Class Test {
    
      protected $em;
    
      public function __construct(EntityManager $entityManager)
      {
        $this->em = $entityManager;
      }
    }
    

    Hope this makes sense

    0 讨论(0)
  • 2020-12-06 06:46

    I think you are in the right direction, I would take the second option:

    For the second option I think that the definition inside routing.yml is wrong

    //routing.yml 
    msd_home_cambiardimensiones:
        pattern: /cambiardimensiones
        defaults: { _controller: msd.imagetrans.controller:cambiardimensionesAction }
    

    Here just remove MSDHomeBundle from the _controller inside defaults

    For the first option:

    Does HomeController has its own constructor?

    //src/MSD/HomeBundle/Resources/config/services.yml
    services:
        imageTransController.custom.service:
            class:  MSD\HomeBundle\Controller\ImageTransController
            arguments: [@doctrine]
    

    it could help then inside the constructor

    __construct(Registry $doctrine)
    $this->doctrine = $doctrine;
    
    or
    
    $this->em = $doctrine->getManager();
    
    0 讨论(0)
  • 2020-12-06 06:48

    Why do you want to grab the Doctrine 2 EntityManager in the constructor of a controller?

    Why not simply do $em = $this->getDoctrine()->getManager(); (or $em = $this->getDoctrine()->getEntityManager(); in Symfony 2.0) in the action(s) you need it? This saves you from the overhead of initializing the EntityManager when you don't need it.

    If you really do want to do this, there are clear instructions on How to define Controllers as Services. Basically it looks like this:

    # src/MSD/HomeBundle/Controller/ImageTransController.php
    
    namespace MSD\HomeBundle\Controller;
    
    use Doctrine\ORM\EntityManager;
    
    class ImageTransController
    {
        private $em;
    
        public function __construct(EntityManager $em)
        {
            $this->em = $em;
        }
    
        public function indexAction()
        {
            // use $this->em
        }
    }
    
    
    # src/MSD/HomeBundle/Resources/config/services.yml
    
    parameters:
        msd.controller.image_trans.class: MSD\HomeBundle\Controller\ImageTransController
    
    services:
        msd.controller.image_trans:
            class:     "%msd.controller.image_trans.class%"
            arguments: ["@doctrine.orm.default_entity_manager"]
    
    # app/config/routing.yml
    
    msd_home_cambiardimensiones:
        path:         /cambiardimensiones
        defaults:     { _controller: msd.controller.image_trans:indexAction }
    
    0 讨论(0)
提交回复
热议问题