Doctrine 2 - 多数据库配置和使用 [英] Doctrine 2 - Multiple databases configuration and use

查看:92
本文介绍了Doctrine 2 - 多数据库配置和使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有MySQL数据库的Symfony2项目:

 #app / config / config.yml 
doctrine :
dbal:
driver:%database_driver%#<
主机:%database_host%#|
port:%database_port%#|定义在
dbname:%database_name%#| parameters.ini
user:%database_user%#|
密码:%database_password%#<

orm:
auto_generate_proxy_classes:%kernel.debug%
auto_mapping:true

现在我想对其他数据库进行简单的查询(例如例程调用)。



我应该将另一个dbal定义到配置文件?

如果是,在保留项目的默认连接的情况下如何配置?

我必须为每个连接配置一个orm?

解决方案

您需要添加另一个级别的配置,并使用多个实体管理员,因为Doctrine每个数据库连接使用1个实体管理器。您的配置可能如下所示:

  doctrine:
dbal:
连接:
默认值:
驱动程序:%database_driver%#<
主机:%database_host%#|
port:%database_port%#|定义在
dbname:%database_name%#| parameters.ini
user:%database_user%#|
密码:%database_password%#<
另一个:
驱动程序:%database2_driver%#<
主机:%database2_host%#|
port:%database2_port%#|定义在
dbname:%database2_name%#| parameters.ini
user:%database2_user%#|
密码:%database2_password%#<

然后,您定义多个实体经理如此






default_entity_manager:默认
entity_managers:
默认值:
连接:默认
映射:
AcmeDemoBundle:〜
AcmeStoreBundle:〜
另一个:
连接:另一个
映射:
AcmeCustomerBundle:〜

然后在您的操作中,您可以使用以下内容获取正确的实体经理:

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

取决于您所需的实体经理


I have a Symfony2 project with a MySQL db:

#app/config/config.yml
doctrine:
    dbal:
        driver:   %database_driver%    # <
        host:     %database_host%      # |
        port:     %database_port%      # | Defined in
        dbname:   %database_name%      # | parameters.ini
        user:     %database_user%      # |
        password: %database_password%  # <

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

Now I'd like to make simple queries (like routine calls) to an other database.

Should I define an other dbal into the config file ?
If yes, how can it be configured while keeping the default connection for the project ?
Do I have to configure an orm for each connection ?

解决方案

You need to add another level of configuration and also use multiple entity managers as Doctrine uses 1 entity manager per database connection .. your configuration might look something like this :

doctrine:
    dbal:
      connections:
        default:
          driver:   %database_driver%    # <
          host:     %database_host%      # |
          port:     %database_port%      # | Defined in
          dbname:   %database_name%      # | parameters.ini
          user:     %database_user%      # |
          password: %database_password%  # <
        another:
          driver:   %database2_driver%    # <
          host:     %database2_host%      # |
          port:     %database2_port%      # | Defined in
          dbname:   %database2_name%      # | parameters.ini
          user:     %database2_user%      # |
          password: %database2_password%  # <

Then you define your multiple entity managers as so

doctrine:
    orm:
        default_entity_manager:   default
        entity_managers:
            default:
                connection:       default
                mappings:
                    AcmeDemoBundle: ~
                    AcmeStoreBundle: ~
            another:
                connection:       another
                mappings:
                    AcmeCustomerBundle: ~

then in your action you can use the following to get the correct entity manager :

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

depending on which entity manager you required

这篇关于Doctrine 2 - 多数据库配置和使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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