How to inject non-default entity managers?

d00*_*001 12 symfony doctrine-orm symfony-2.1

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

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

$customerEm =  $this->get('doctrine')->getManager('customer');
Run Code Online (Sandbox Code Playgroud)

We can inject the default manager to any service by using:

"@doctrine.orm.entity_manager"
Run Code Online (Sandbox Code Playgroud)

How can you inject non-default entity managers into services?

Mun*_*Das 29

如果您的实体经理配置名称,non_default那么您可以将其引用为@doctrine.orm.non_default_entity_manager

  • 你也可以发出`php app/console container:debug`命令来验证它. (2认同)
  • 是的,它正在运行,它比我的解决方案更好 - 所以如果有可能使用stackoverflow更改已接受的答案,那么这个应该被接受.最好! (2认同)

Jul*_*ent 5

对于使用 Symfony 3+ 的用户,请使用控制台: php bin/console debug:container

然后你应该看到许多以 'doctrine.orm.MY_CUSTOM_ENTITY_MANAGER_xxxxxxxxxx' 开头的行

因此,如果您想要与您的自定义实体管理器相对应的实体管理器,请找到以下行:'doctrine.orm.MY_CUSTOM_ENTITY_MANAGER_entity_manager'

您可以将它插入到您的服务参数中。

希望能帮助到你。