Php 如何使用多个db设置symfony 3原则迁移?

Php 如何使用多个db设置symfony 3原则迁移?,php,symfony,doctrine-orm,doctrine-migrations,Php,Symfony,Doctrine Orm,Doctrine Migrations,在验证和更新模式时,我很难让symfony/doctrine排除db视图 我第一次尝试了无原则迁移(),但没有成功 我发现条令迁移会从验证/更新中过滤出视图,事实上确实如此,所以这一步似乎适用于迁移 因此,如果一个数据库只有一个,那么原则迁移就可以正常工作,但至少可以说,使用多个数据库的设置并不是一刀切的 这是一个大家都知道的问题,你可以在上面看到。不幸的是,当试图遵循链接中描述的解决方案时,结果是混乱的 尽管migrations:update--em=default命令将指示设置了正确的数据库

在验证和更新模式时,我很难让symfony/doctrine排除db视图

我第一次尝试了无原则迁移(),但没有成功

我发现条令迁移会从验证/更新中过滤出视图,事实上确实如此,所以这一步似乎适用于迁移

因此,如果一个数据库只有一个,那么原则迁移就可以正常工作,但至少可以说,使用多个数据库的设置并不是一刀切的

这是一个大家都知道的问题,你可以在上面看到。不幸的是,当试图遵循链接中描述的解决方案时,结果是混乱的

尽管migrations:update--em=default命令将指示设置了正确的数据库,但在生成migrations:diff--em=default时,它将与其他数据库混合,同样地,也会与migrations:migrate--em=default混合,后者最终会在其他数据库上创建表

更具体地说,错误是: -它将为迁移文件创建单独的目录,如配置文件中所示,但不会创建到相应的em -它将生成混合了这两个em的mysql查询 -因此,它将更新数据库

我的设置如下:

config.yml

imports:
....
- { resource: doctrine_migrations_default.yml }
- { resource: doctrine_migrations_used.yml }

doctrine:
  dbal:
    default_connection: default
    connections:
        default:      
            .....
            #schema_filter: "~^(?!view).*$~"
            schema_filter: ~^(?!view_)~
        used:
             ......

orm:
    auto_generate_proxy_classes: '%kernel.debug%'
    default_entity_manager: default
    entity_managers:
        default:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            connection: default
            auto_mapping: true
            mappings:
                AppBundle:  ~
        used:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            connection: used
            mappings:
                UsedBundle: ~ 
然后,迁移的特定配置文件是:

原则_迁移_default.yml

doctrine_migrations: 
        dir_name: "%kernel.root_dir%/DoctrineMigrationsDefault"
        namespace: App\DoctrineMigrationsDefault
        table_name: migration_versions
        name: Application_Migrations_Default 
migrations_directory: "app/DoctrineMigrationsDefault"
migrations_namespace: App\DoctrineMigrationsDefault
table_name: migration_versions
name: Application_Migrations_Default 
原则_迁移_used.yml

doctrine_migrations:
    dir_name: "%kernel.root_dir%/DoctrineMigrationsUsed"
    namespace: Used\DoctrineMigrationsUsed
    table_name: migration_versions
    name: Application Migrations Used
    organize_migrations: false
migrations_directory: "app/DoctrineMigrationsUsed"
migrations_namespace: Used\DoctrineMigrationsUsed
table_name: migration_versions
name: Application Migrations Used
organize_migrations: false #valid entries are false, 'year', and 'year_and_month'
这是它如何混合配置的一个示例。数据库名称是正确的。它对应于em=默认值。但其他信息来自em=used

谢谢

php bin/console doctrine:migrations:status --em=default
==配置

>> Name:                                               Application Migrations Used
>> Database Driver:                                    pdo_mysql
>> Database Name:                                      symfony_cars
>> Configuration Source:                               manually configured
>> Version Table Name:                                 migration_versions
>> Version Column Name:                                version
>> Migrations Namespace:                               Used\DoctrineMigrationsUsed
>> Migrations Directory:                               /Users/BAMAC/Sites/Symfony1/app/DoctrineMigrationsUsed
>> Previous Version:                                   Already at first version
>> Current Version:                                    0
>> Next Version:                                       2017-10-19 08:03:52 (20171019080352)
>> Latest Version:                                     2017-10-19 08:03:52 (20171019080352)
>> Executed Migrations:                                0
>> Executed Unavailable Migrations:                    0
>> Available Migrations:                               1
>> New Migrations:                                     1
此外,如果我尝试使用以下命令明确指示配置文件:

php bin/console原则:迁移:状态--em=default--configuration=./app/config/document\u migrations\u default.yml

doctrine_migrations: 
        dir_name: "%kernel.root_dir%/DoctrineMigrationsDefault"
        namespace: App\DoctrineMigrationsDefault
        table_name: migration_versions
        name: Application_Migrations_Default 
migrations_directory: "app/DoctrineMigrationsDefault"
migrations_namespace: App\DoctrineMigrationsDefault
table_name: migration_versions
name: Application_Migrations_Default 
它不会识别文件信息,即使它直接从config.yml获取信息时会识别。将引发以下错误

[条令\DBAL\Migrations\MigrationException]
迁移配置密钥“doctrine_Migrations”不存在


如果我取出doctrine_migrations密钥,它将在下次遇到信息时生成错误。

不要在config.yml文件中导入迁移设置

您的各个配置文件实际上没有正确配置,这就是为什么您会收到关于配置密钥不存在的错误。密钥与正常迁移配置中的密钥不同。我必须搜索代码才能找到正确的设置。(我在35号公路附近发现的)

试试这些-

原则_迁移_default.yml

doctrine_migrations: 
        dir_name: "%kernel.root_dir%/DoctrineMigrationsDefault"
        namespace: App\DoctrineMigrationsDefault
        table_name: migration_versions
        name: Application_Migrations_Default 
migrations_directory: "app/DoctrineMigrationsDefault"
migrations_namespace: App\DoctrineMigrationsDefault
table_name: migration_versions
name: Application_Migrations_Default 
原则_迁移_used.yml

doctrine_migrations:
    dir_name: "%kernel.root_dir%/DoctrineMigrationsUsed"
    namespace: Used\DoctrineMigrationsUsed
    table_name: migration_versions
    name: Application Migrations Used
    organize_migrations: false
migrations_directory: "app/DoctrineMigrationsUsed"
migrations_namespace: Used\DoctrineMigrationsUsed
table_name: migration_versions
name: Application Migrations Used
organize_migrations: false #valid entries are false, 'year', and 'year_and_month'
条令迁移、目录名称和命名空间不是该配置文件的有效条目


此外,您不能在目录路径中使用%kernel.root\u dir%,但对我有效的方法是将其更改为“app”或提供完整路径。

将此留作将来参考:

文件doctor_migrations_default.yaml和doctor_migrations_used.yaml用于在多个实体管理器之间执行迁移时使用的额外配置

symfony不应自动加载这些文件,您可以将它们直接放在config/下,而不是放在doctor_migrations.yaml所在的config/包中

我在symfony 5.0.4上测试了它,它工作得非常好:


php bin/console原则:迁移:迁移--em=used--configuration=config/doctor\u migrations\u used.yaml

我遇到并解决了一个类似的问题,请参阅