Doctrine2 Symfony2 for multiple databases

I am having trouble porting a db to a non-default database with Symfony2 and Doctrine.

I have two dbs and two entity managers that I work with. I have two bundles, each working with a corresponding EM, so everything should be completely separated.

In my DefaultBundle, I have migrations with about 20 migration files, all of which are in the default db. My SecondBundle has one migration file that applies to seconddb.

When I try to run

   php app/console doctrine:migrations:migrate --no-interaction --em="second"

      

I get

Migration 20140709212101 failed during Execution. Error There is no table with name 'second_database.users' in the schema.

  [Doctrine\DBAL\Schema\SchemaException]                                  
  There is no table with name 'second_database.users' in the schema.  

      

Which is quite right, "users" is a table in my main db. What's going on here is that the migration file it is trying to run is actually the first of 20 in the DefaultCore (which are all old and already applied). As I pointed out - this is the intersection of migrations I want, do not want to work with the db I am working with. There seems to be no other link option to specify a set, for example for other migration commands like generate. I want to ignore the DefaultBundle and just run my migrations from my SecondBundle.

+3


source to share


2 answers


The solution for this is you need to run migrations twice, once on every -em, and then in the migration itself check which database you are in to see if it should be run or not. You are doing a ContainerAware migration so that you have access to this. Example:



class Version201501021322 extends AbstractMigration implements ContainerAwareInterface
{
    private $container;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    /**
     * @param Schema $schema
     */
    public function up(Schema $schema)
    {
        $this->skipIf($this->connection->getDatabase() != $this->container->getParameter('second_database_name'), 'Skipping database.');

        $this->addsql("//migration here");
    }


}

      

+2


source


I recently came across this problem too.

The problem came when it doctrine:fixtures:load

tries to flush the database for that entity manager that you are passing in, but it fails because it cannot find a specific table that is stored in another database.

To avoid this problem, you can use the key --append

. For example:

$ php app/console doctrine:fixtures:load --em=second --append

      

This key means that your database will not be cleared, so the fixtures will load over the existing data.



This only works if the bindings are loaded only for the connection second

.

I think you can avoid this by going to the --fixtures

key, the fixtures directory (but I'm not sure).


In my case, I have 2 databases and 2 connections ( default

, not_default

), but I only have bindings to connect default

, so this works fine for me.

0


source







All Articles