具有多个实体管理器的 symfony 学说迁移

symfony doctrine migrations with multiple entity managers

我正在尝试制作 SaaS 应用程序,为此目的,我按照 official documentation 创建了多个实体管理器和相应的连接,但是 运行 php bin/console doctrine:migrations:diff --em=customer 命令导致异常 The "--em" option does not exist..我知道我可以更改名为 emdoctrine_migrations.yaml 参数,但考虑到实体管理器的数量可能会增加,这不是解决方案。有什么解决方法吗?

doctrine.yml:

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                url: '%env(resolve:DATABASE_URL)%'
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
            tenant:
                url: '%env(resolve:TENANT_DATABASE_URL)%'
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                wrapper_class: App\Service\Config\Database\Connection

    orm:
        auto_generate_proxy_classes: true

        default_entity_manager: default
        entity_managers:
            default:
                auto_mapping: false
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                mappings:
                    Shared:
                        mapping: true
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Shared'
                        prefix: 'App\Entity\Shared'
                        alias: Shared

            tenant:
                connection: tenant
                auto_mapping: false
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                mappings:
                    Tenant:
                        mapping: true
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Tenant'
                        prefix: 'App\Entity\Tenant'
                        alias: Tenant

App\Service\Config\Database\Connection class 只是一个可以动态更改连接参数的包装器。

到目前为止的答案是:文档是 outdated, doctrine/doctrine-migrations-bundle v3.1 will probably solve it, but it relies on doctrine/migrations library to release v3.1 (as mentioned in issue), and so far only solution is this 使用命令包装器的解决方法,但它需要对配置进行调整(文章中的 YamlFile 需要教义风格的配置而不是 symfony 的)

您必须为每个连接编写一个 config.yml,然后您可以使用 php bin/console doctrine:migration:diff --configuration=<path-to-config>/config.yml 来迁移您的数据库。

doctrine_migrations:
    migrations_paths:
        'DoctrineMigrations': 'src/MigrationsDefault'
    connection: default
doctrine_migrations:
    migrations_paths:
        'DoctrineMigrations': 'src/MigrationsTenant'
    connection: tenant