Symfony2 Teaching: Schema: Upgrading with Multiple Entity Managers

I have two database connections in config.yml and two entity managers. Each one is tied to a bundle.

The problem I am facing is doing unit tests that start by creating an empty database and loading the data. It creates both databases, but I am getting both sets of tables in each database instead of one set of objects in one db and the other in the other. Since the two db connections are not incredibly common, I am having trouble finding help.

doctrine:
    dbal:
        default_connection: default
 ...
    connections:
         default:
         (conectioninfo)
         seconddb:
         (connectioninfo)


 orm:
    default_entity_manager: default
    auto_generate_proxy_classes: "%kernel.debug%"
    entity_managers:
    default:
        connection: default
        mappings:
           MycompanyCoreBundle: ~
    seconddb:
        connection: seconddb
        mappings:
           MycomanySecondBundle: ~

      

When doing unit tests, the lines I have are as follows:

php app/console doctrine:database:drop --force --env=test --connection=default 
php app/console doctrine:database:create --env=test --connection=default
php app/console doctrine:schema:drop --force --no-interaction --env=test --em=default
php app/console doctrine:schema:update --force --no-interaction --env=test --em=default

php app/console doctrine:database:drop --force --env=test --connection=seconddb
php app/console doctrine:database:create --env=test --connection=seconddb
php app/console doctrine:schema:drop --force --no-interaction --env=test --em=seconddb
php app/console doctrine:schema:update --force --no-interaction --env=test --em=seconddb

      

When doing it all, the exit

Successfully deleted cache entries.
Dropping database schema...
Database schema dropped successfully!
Updating database schema...
Database schema updated successfully! "91" queries were executed

      

The problem is that these 91 queries are a combination of both Entity folders in two packages. I am missing somewhere to specify them separately so that they enter the appropriate databases.

+3


source to share


1 answer


I ended up finding the answer. There is no way to specify the database to migrate, so you basically need to migrate twice to EM and then check the db connection.

Inside each migration file, you put a line to ignore it if it is not correct. Thus, some files have

$this->skipIf( $this->connection->getDatabase() != 'dbone', 'Skipping database.' );

      

and others have



$this->skipIf( $this->connection->getDatabase() != 'dbtwo', 'Skipping database.' );

      

Then when you run these commands:

doctrine:migrations:migrate --em="default"
doctrine:migrations:migrate --em="orm"

      

Both will loop through all your migration files, but those that don't apply to this situation will be ignored.

+1


source







All Articles