一个实体管理器和多个数据库未检测到模式更改的学说

2022-01-16 00:00:00 php symfony doctrine doctrine-orm

使用 Symfony-2.1 和 Doctrine-2.3.我有多个数据库,需要进行跨数据库连接,所以我遵循了以下建议:

Using Symfony-2.1 and Doctrine-2.3. I have multiple databases and need to do cross-database joins so I followed the suggestions in:

  • Doctrine2 和 Zend 框架中的多数据库连接
  • 使用与多个实体经理的关系

并设置多个连接和一个实体管理器.这是app/config/config.yml:

and set up multiple connections and one entity manager. Here is app/config/config.yml:

doctrine:
    dbal:
        default_connection: db1
        connections:
            db1:
                driver:   pdo_mysql
                host:     127.0.0.1
                dbname:   db1
            db2:
                driver:   pdo_mysql
                host:     127.0.0.1
                dbname:   db2
    orm:
        default_entity_manager: default
        auto_generate_proxy_classes: %kernel.debug%
        entity_managers:
            default:
                auto_mapping: true
                mappings:
                    FirstBundle:
                        type:   annotation
                        dir:    Model
                        prefix: NoiseLabsFirstBundleModel
                    SecondBundle:
                        type:   annotation
                        dir:    Model
                        prefix: NoiseLabsSecondBundleModel

FirstBundle中的实体类:

namespace NoiseLabsFirstBundleModel;

/**
 * @ORMEntity
 * @ORMTable(name="db1.table1")
 */
class FirstEntity
{
    /**
     * @ORMId
     * @ORMColumn(name="id", type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;
}

SecondBundle中的实体类:

namespace NoiseLabsSecondBundleModel;

/**
 * @ORMEntity
 * @ORMTable(name="db2.table2")
 */
class SecondEntity
{
    /**
     * @ORMId
     * @ORMColumn(name="id", type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORMManyToOne(targetEntity="NoiseLabsFirstBundleModelFirstEntity")
     * @ORMJoinColumn(name="firstId", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $first;
}

现在,问题是 app/console dictionary:schema:update --dump-sql 仅检测(默认连接)db1.tables 中的架构更改.如果我将默认连接设置为 db2 它只会输出与 db2.tables 相关的 sql.

Now, the problem is app/console doctrine:schema:update --dump-sql only detects schema changes in (default connection) db1.tables. If I set the default connection to db2 it will only output sql related to db2.tables.

我已经检查了 app/console dictionary:mapping:info 并且正在映射所有实体类.

I've checked with app/console doctrine:mapping:info and all entity classes are being mapped.

这是 Doctrine/Symfony 的限制还是我需要调整我的配置?谢谢.

Is this a limitation of Doctrine/Symfony or do I need to adjust my configuration? Thanks.

推荐答案

每个实体管理器只能有一个连接.将默认实体管理器一分为二.app/console dictionary:schema:update 采用可选参数 (em) 来指定正在使用的实体管理器.你必须运行它两次:

Each entity manager can have only one connection. Split the default entity manager into two. app/console doctrine:schema:update takes an optional argument (em) to specify the entity manager in use. You'd have to run it twice:

app/console doctrine:schema:update --dump-sql em="default"
app/console doctrine:schema:update --dump-sql em="my_second_em"

还有一篇短文(如何使用多个实体管理器和连接) 在值得一读的 Symfony2 食谱中.

There's also a short article (How to work with Multiple Entity Managers and Connections) in the Symfony2 cookbook that's worth reading.

相关文章