How to use Symfony autowiring with multiple entity managers

狂风中的少年 提交于 2020-03-22 09:13:09

问题


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 TestService
{
    public function __construct(EntityManager $emA, EntityManager $emB)
    {
    }
}

My service.yml file use to be configured like that :

    app.testservice:
        class: App\Services\TestService
        arguments:
            - "@doctrine.orm.default_entity_manager"
            - "@doctrine.orm.secondary_entity_manager"

回答1:


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)
    {
        …
    }
}



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/59266372/how-to-use-symfony-autowiring-with-multiple-entity-managers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!