How to use Symfony autowiring with multiple entity managers

后端 未结 3 919
旧时难觅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: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.

提交回复
热议问题