如何在多个实体管理器中使用Symfony自动装配 [英] How to use Symfony autowiring with multiple entity managers

查看:60
本文介绍了如何在多个实体管理器中使用Symfony自动装配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用2个不同实体管理器的服务中使用自动装配.如何实现这样的目标?

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

我的service.yml文件用于这样配置:

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"

推荐答案

Dylan的答案违反了Demeter定律.从Symfony 3.4开始,这非常简单而优雅,可以满足本地服务绑定:

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

这篇关于如何在多个实体管理器中使用Symfony自动装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆