如何在Symfony2中注入具体的学说实体经理? [英] How to inject specific Doctrine Entity Manager in Symfony2?

查看:130
本文介绍了如何在Symfony2中注入具体的学说实体经理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用相同数据库的几个项目中,我们制作了一个Symfony2 Bundle来映射所有常用的功能。

Working on several projects which use the same database, we made a Symfony2 Bundle to map all common functions.

现在的问题是我们有一个第二个数据库,我们需要同样的服务,就像第一个。

Now the issue is that we have a second database, and we need the same kind of service, just as the first one.

config.yml

doctrine:
  dbal:
    default_connection: main
    connections:
      main:
        /* ... */
      sub:
        /* ... */

  orm:
      default_entity_manager:   main
      entity_managers:
          main:
              connection:       main
              mappings:
                  AcmeMainBundle: ~
          sub:
              connection:       sub
              mappings:
                  AcmeSubBundle: ~
      auto_generate_proxy_classes: %kernel.debug%

@AcmeMainBundle> services.yml

services:
    mainmanager:
      class: Acme\MainBundle\MainManager
      arguments: [ @doctrine.orm.entity_manager ]

Acme\MainBundle\MainManager

class MainManager
{
    public function __construct(EntityManager $em)
    {
        $em->getRepository('AcmeMainBundle:Foo');
    }
}

这套工作正常,我得到了所有预期的结果, default_entity_manager 设置为 main ,这是正确的EntityManager。

This set works fine, I get all expected results since default_entity_manager is set to main which is the right EntityManager.

但现在这里是这个问题。

But now here's the issue.

@AcmeSubBundle> services.yml

submanager:
  class: Acme\SubBundle\SubManager
  arguments: [ @doctrine.orm.entity_manager ]

Acme\SubBundle\子管理器

class SubManager
{
    public function __construct(EntityManager $em)
    {
        $em->getRepository('AcmeSubBundle:Bar'); // Throws exception
    }
}




未知实体命名空间别名AcmeSubBundle

Unknown entity namespace alias AcmeSubBundle

由于 EntityManager 进入主要

我的问题是,是否有一个干净的方式来注入特定的实体经理作为 services.yml 中的参数

My question is, is there a "clean" way to inject a specific entity manager as argument in services.yml ?

推荐答案


/ 02/13

Answer at 18/02/13

这是一种通过特定实体管理器从 services.yml

学说相对于他们的名字生成一个新的服务名称。

Here is a way to pass a specific entity manager from services.yml
Doctrine generates a new service name relative to their names.

示例:

@doctrine.orm.default_entity_manager

在这个案例,它生成2个其他实体经理

In this case, it generates 2 others entity manager

@doctrine.orm.main_entity_manager
@doctrine.orm.sub_entity_manager

参数pas sed是一个 Doctrine\ORM\EntityManager 对象

The argument passed is a Doctrine\ORM\EntityManager object

在我的情况下:

services.yml

submanager:
    arguments: [ @doctrine.orm.sub_entity_manager ]








更新22/08/13

Update 22/08/13

另一种替代方法是直接给存储库而不是经理。

An alternative to this would be to directly give the repository instead of the manager.

为此,您必须创建一个将保存存储库的服务。

To do such, you must create a service which will hold the repository.

services.yml

services:
    acme.foo_repository:
        class: Doctrine\Common\Persistence\ObjectRepository
        factory_service: doctrine.orm.main_entity_manager
        factory_method:  getRepository
        arguments:
            - "AcmeMainBundle:Foo"

我们让学说生成给定的回购存档。

然后我们可以将其注入到另一个服务中。

We let doctrine generate the given repository.
Then we can inject it in another service

services:
    mainmanager:
        class: Acme\MainBundle\MainManager
        arguments:
            - @acme.foo_repository

这篇关于如何在Symfony2中注入具体的学说实体经理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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