How to use Symfony autowiring with multiple entity managers

后端 未结 3 872
旧时难觅i
旧时难觅i 2021-01-21 14:00

I would like to use the autowiring in a service that use 2 different entity manager. How to achieve something like that ?

use Doctrine\\ORM\\EntityManager;
class         


        
相关标签:
3条回答
  • 2021-01-21 14:25

    Dylan's answer violates the Demeter's Law principle. It's very easy and elegant since Symfony 3.4, meet Local service binding:

    services:
      _defaults:
        bind:
          $emA: "@doctrine.orm.default_entity_manager"
          $emB: "@doctrine.orm.secondary_entity_manager"
    

    Then in your service the autoloading will do the hard work for you:

    class TestService
    {
        public function __construct(EntityManager $emA, EntityManager $emB)
        {
            …
        }
    }
    
    0 讨论(0)
  • 2021-01-21 14:41

    The easy way would be to autowire ManagerRegistry in your constructor and use it to get the managers you want by using the names of the entity manger you have set in your configuration file (doctrine.yaml) :

    use Doctrine\Common\Persistence\ManagerRegistry;
    class TestService
    {
    
        private $emA;
        private $emB;
        public function __construct(ManagerRegistry $doctrine)
        {
             $this->emA = $doctrine->getManager('emA');
             $this->emB = $doctrine->getManager('emB');
        }
    }
    

    And you should be able to use them as you want.


    Another way would be to follow this answer by Ron Mikluscak

    0 讨论(0)
  • 2021-01-21 14:46

    There are already two good answers posted but I'd like to add a third as well as some context to help chose which approach to use in a given situation.

    emix's answer is very simple but a bit fragile in that it relies on the name of the argument for injecting the correct service. Which is fine but you won't get any help from your IDE and sometimes might be a bit awkward. The answer should probably use EntityManagerInterface but that is a minor point.

    DynlanKas's answer requires a bit of code in each service to locate the desired manager. It's okay but can be a bit repetitive. On the other hand, the answer is perfect when you don't know in advance exactly which manager is needed. It allows you to select a manager based on some dynamic information.

    This third answer is largely based on Ron's Answer but refined just a bit.

    Make a new class for each entity manager:

    namespace App\EntityManager;
    use Doctrine\ORM\Decorator\EntityManagerDecorator;
    class AEntityManager extends EntityManagerDecorator {}
    class BEntityManager extends EntityManagerDecorator {}
    

    Don't be alarmed that you are extending a decorator class. The class has the same interface and the same functionality as a 'real' entity manager. You just need to inject the desired manager:

    # config/services.yaml
    App\EntityManager\AEntityManager:
        decorates: doctrine.orm.a_entity_manager
    
    App\EntityManager\BEntityManager:
        decorates: doctrine.orm.b_entity_manager
    

    This approach requires making a new class for each entity manager as well as a couple of lines of configuration, but allows you to simply typehint against the desired class:

    public function __construct(AEntityManager $emA, BEntityManager $emB)
    {
    }
    

    It is, arguably, the most robust and standard way to approach the original question.

    0 讨论(0)
提交回复
热议问题