【问题标题】:How to create 2 connections (mysql and postgresql) with Doctrine2 in Symfony2如何在 Symfony2 中使用 Doctrine2 创建 2 个连接(mysql 和 postgresql)
【发布时间】:2011-12-09 02:41:12
【问题描述】:

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

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

【问题讨论】:

    标签: symfony doctrine-orm


    【解决方案1】:

    你可以这样做:

    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 一个中声明了两个实体管理器。

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

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

    由于alpha 被定义为默认值,您无需指定名称即可访问它:

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

    【讨论】:

    • 谢谢!但是如果我只在读取模式下使用某些数据库,是否需要使用实体类中的setter方法?
    • 需要我在实体类中为现有数据库(只读模式)指定字段类型吗?
    • 是的,因为 Doctrine 需要这些信息来正确地滋润你的实体......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    相关资源
    最近更新 更多