如何在 Symfony2 中使用 Doctrine2 创建 2 个连接(mysql 和 postgresql) [英] How to create 2 connections (mysql and postgresql) with Doctrine2 in Symfony2

查看:24
本文介绍了如何在 Symfony2 中使用 Doctrine2 创建 2 个连接(mysql 和 postgresql)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Symfony2 的新手.我的问题很简单.我会在一个包中使用 2 个到不同主机和驱动程序的 DB 连接.

I'm new to Symfony2. And my question quite simple. I would use 2 connections to DB at different host and driver in one bundle.

你能帮我解决这个问题吗?

Could you help me with this?

推荐答案

您可以执行以下操作:

doctrine:
    dbal:
        default_connection: alpha
        connections:
            alpha:
                driver:     pdo_mysql
                host:       localhost
                dbname:     alpha
                user:       root
                charset:    UTF8
            beta:
                driver:     pdo_pgsql
                host:       localhost
                dbname:     beta
                user:       root
                charset:    UTF8
    orm:
        auto_generate_proxy_classes: %kernel.debug%
        entity_managers:
            alpha:
                connection: alpha
            beta:
                connection: beta

你看,我们在 dbal 部分声明了两个连接,在 orm 部分声明了两个实体管理器.

You see, we declare two connections in the dbal section and two entity managers in the orm one.

之后,您可以同时使用两者:

After that, you can use both:

$emAlpha = $this->getDoctrine()->getEntityManager('alpha');
$emBeta  = $this->getDoctrine()->getEntityManager('beta');

由于 alpha 一个被定义为默认的,你可以不用指定它的名字来访问它:

As the alpha one was defined as the default one, you can access it without specifying its name:

$emAlpha = $this->getDoctrine()->getEntityManager();

这篇关于如何在 Symfony2 中使用 Doctrine2 创建 2 个连接(mysql 和 postgresql)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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