Symfony3:在服务中注入几个@ doctrine.orm.entity_manager

时间:2018-10-21 18:52:21

标签: symfony

我与several connexion

这是我的config.yml:

doctrine:
    dbal:
        default_connection: connexion_1# specify the connexion used by default
        connections:
            connexion_1:
                driver:   "%database_driver1%"  
                 ....
            connexion_2:
                driver:    "%database_driver2%" 
                 ...

我想在服务中注入2个connexion实体管理器。

在我的service.yml中,我只能注入一个名为“ @ doctrine.orm.entity_manager”的entity_manager。这是默认的经理

@doctrine.orm.entity_manager argument
    zip_command:
        class: BackEndBundle\Command\ZipCommand
        arguments:
            - '@doctrine.orm.entity_manager'
        tags:
            -  { name: console.command }         

如何注入其他连接?

1 个答案:

答案 0 :(得分:0)

选项1

根据文档,您应该能够通过doctrine.orm.{entity_manager_name_here}_entity_manager别名注入自定义实体管理器。您可以在config.yml中的doctrine.orm.entity_managers下找到实体经理的姓名。

选项2

如果您想访问服务中的所有实体管理器,则可以直接注入Doctrine\Common\Persistence\ManagerRegistry并使用getManager(s)方法。