如何在Symfony2 Doctrine2中获取不同的DBAL连接 [英] How to get different DBAL Connection in Symfony2 Doctrine2

查看:100
本文介绍了如何在Symfony2 Doctrine2中获取不同的DBAL连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two connection in my config.yml file

doctrine:
      dbal:
         default:
         connection2

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

Then in my class , i use this

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

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

But it is getting the default connection. How can i use the second Connection

可以从服务中使用它。

推荐答案

您必须在config.yml

You have to define both a DBAL connection and an entity manager in the config.yml

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

不,您可以使用以下方式访问实体管理员:

No you can access the Entity mangager with:

$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

这篇关于如何在Symfony2 Doctrine2中获取不同的DBAL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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