简体   繁体   English

如果我们有多个数据库和连接,如何加载学说装置

[英]How to load doctrine fixtures if we have multiple databases and connections

I used this documentation : https://symfony.com/doc/4.4/doctrine/multiple_entity_managers.html我使用了这个文档: https : //symfony.com/doc/4.4/doctrine/multiple_entity_managers.html

So now I can create new databases named legacy and Project like this所以现在我可以像这样创建名为 legacy 和 Project 的新数据库

doctrince.yaml: doctrince.yaml:

doctrine:
  dbal:
    default_connection: legacy
    connections:
      legacy:
        driver: pdo_mysql
        host: "%env(database_host)%"
        port: "%env(database_port)%"
        dbname: "%env(database_name)%"
        user: "%env(database_user)%"
        password: "%env(database_password)%"
        charset: UTF8
      project:
        driver: pdo_mysql
        host: "%env(database_host_project)%"
        port: "%env(database_port_project)%"
        dbname: "%env(database_name_project)%"
        user: "%env(database_user_project)%"
        password: "%env(database_password_project)%"
        charset: UTF8
  orm:
    default_entity_manager: legacy
    entity_managers:
      legacy:
        connection: legacy
        mappings:
          Legacy:
            is_bundle: false
            type: annotation
            dir: "%kernel.project_dir%/..."
            prefix: '...'
            alias: Legacy
      project:
        connection: project
        auto_mapping: true
        mappings:
          Project:
            is_bundle: false
            type: annotation
            dir: "%kernel.project_dir%/src/Entity"
            prefix: 'App\Entity'
            alias: Project

Now I have several Fixture classes all of them depend on each other and also some will create fixture for legacy and some will create for project.现在我有几个 Fixture 类,它们都相互依赖,而且有些会为遗留物创建夹具,有些会为项目创建。

Now my question is when I do:现在我的问题是我什么时候做:

php bin/console doctrine:fixtures:load --em=legacy

It runs the appfixtures of the project but not the of the bundle.它运行项目的 appfixtures 但不运行 bundle 的 appfixtures。 and then i get this error:然后我收到此错误:

The class .. was not found in the chain configured namespaces ....在链配置的命名空间中找不到类 ..

My question is now how can I load fixtures on multiple databases with multiple connections.我现在的问题是如何在具有多个连接的多个数据库上加载装置。 Thanks in advance.提前致谢。

You should read this doc : https://symfony.com/doc/current/doctrine/multiple_entity_managers.html您应该阅读此文档: https : //symfony.com/doc/current/doctrine/multiple_entity_managers.html

I think it may solve your problem as this is exactly what you trying to do.我认为它可以解决您的问题,因为这正是您想要做的。

If you have to create a fixture for legacy just use :如果您必须为旧版创建夹具,请使用:

$entityManager = $this->getDoctrine()->getManager('legacy');`

else if it is for project use否则如果是项目使用

$entityManager = $this->getDoctrine()->getManager('project');`

Also, if you change doctrine.dbal.default_connection , entityManager will run against the defined connection.此外,如果您更改doctrine.dbal.default_connection , entityManager 将针对定义的连接运行。

So you can try to create group of fixtures .所以你可以尝试创建一组灯具

And then run fixtures for project :然后为project运行装置:

  1. Edit config for test environment修改测试环境配置
#config/packages/test/doctrine.yaml
doctrine:
    dbal:
        default_connection: 'project'
  1. Then run fixture for 'project' group然后为“项目”组运行夹具
> php bin/console doctrine:fixtures:load --group=project

Then do the same thing for 'legacy'然后对“遗产”做同样的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM