How to inject non-default entity managers?

前端 未结 4 808
太阳男子
太阳男子 2020-12-28 19:01

In Symfony2 you can work with multiple entity managers and use something like the code below:

$em = $this->get(\'doctrine\')->getManager();
$em = $this         


        
4条回答
  •  难免孤独
    2020-12-28 19:51

    You should define your custom entity manager as a service:

    services:  
        name_of_your_custom_manager:
            class: %doctrine.orm.entity_manager.class%
            factory_service:  doctrine
            factory_method:   getEntityManager
            arguments: ["name_of_your_custom_manager"]
    

    Then, you can inject it in the same way as you do with every service:

    @name_of_your_custom_manager
    

    Edit:

    Pay attention that factory method may differ between symfony's version (it could be getEntityManager or getManager)

提交回复
热议问题