使用Symfony迁移迁移多个数据库,而不指定数据库名称

时间:2017-09-22 19:43:28

标签: symfony doctrine doctrine-migrations flynn

我的Symfony项目中有四个不同的数据库。在本地,这些数据库具有各自的名称,因此在迁移中可以将它们区分开来。我在每次迁移中都使用db_name.table为每个表做了前缀,以便迁移正确的数据库。

现在我想在自动生成数据库名称的远程设置(使用flynn.io),但是在所有迁移中都修复了数据库名称...有没有办法从中读取数据库名称环境变量或类似的解决方案?

1 个答案:

答案 0 :(得分:0)

您可以使用DoctrineMigrationsBundle

public function up(Schema $schema)
{
    // ... empty function/ no migrations commands
}


public function postUp(Schema $schema)
{

    // get datebase name here, and then pass this database names to another function
   $converter = $this->container->get('db_names_from_paratmers.yml file');
   $this->initDatabase('db1');
}

public function initDatabase($dbname)
{
  $this->addSql('CREATE TABLE '.$dbname.'office (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
}

请参阅,如果这在某种程度上满足了您的需求,您只需要找到数据库名称并嵌入paramters.yml文件中。