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

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

问题描述

我很喜欢Symfony2。而我的问题很简单。
我将使用两个连接到不同的主机和驱动程序在一个捆绑的数据库。



你能帮助我吗?

解决方案

您可以执行以下操作:

  doctrine:
dbal:
default_connection:alpha
连接:
alpha:
驱动程序:pdo_mysql
主机:localhost
dbname:alpha
用户:root
charset:UTF8
beta:
驱动程序:pdo_pgsql
主机:localhost
dbname:beta
用户:root
charset:UTF8
orm:
auto_generate_proxy_classes:%kernel.debug%
entity_managers:
alpha:
connection:alpha
beta:
connection:beta

你看,我们在 dbal 部分中声明两个连接和 orm 中的两个实体管理员。



之后,您可以同时使用:

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

由于 alpha 一个被定义为默认情况下,您可以访问它,而不指定其名称:

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


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?

解决方案

You can do something like:

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

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');

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天全站免登陆