简体   繁体   中英

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

So now I can create new databases named legacy and Project like this

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.

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. 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

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.

So you can try to create group of fixtures .

And then run fixtures for 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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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