2

我的 config.yml 文件中有两个连接

doctrine:
      dbal:
         default:
         connection2

然后在我的课上,我用这个

$em = $this->container->get('doctrine')->getEntityManager();

但它正在获得默认连接。我如何使用第二个连接

我可以从服务中使用它吗?

4

1 回答 1

3

您必须在 config.yml 中定义 DBAL 连接和实体管理器

doctrine:
    dbal:
        default_connection:   connection1
        connections:
            connection1:
                ...
            connection2:
                ...
    orm:
        default_entity_manager: em1
        entity_managers:
            em1:
                 connection: connection1
                 ....
            em2:
                 connection: connection2

不,您可以通过以下方式访问实体管理器:

$em = $this->container->get('doctrine')->getEntityManager();
// Returns $em1/connection1

$em = $this->container->get('doctrine')->getEntityManager('em1');
// Returns $em1/connection1

$em = $this->container->get('doctrine')->getEntityManager('em2');
// Returns $em2/connection2
于 2012-08-14T07:29:12.097 回答