2017-09-22 32 views
0

我的Symfony項目中有四個不同的數據庫。在本地,這些數據庫有它們各自的名稱,因此在遷移時可以區分它們。我在每次遷移時都使用db_name.table爲每個表添加前綴,以便遷移正確的數據庫。使用Symfony遷移遷移多個數據庫,無需指定數據庫名稱

現在我想在遠程數據庫名稱自動生成(使用flynn.io),但數據庫名稱是固定的所有遷移...有一種方法來讀取數據庫名稱從環境變量或一些類似的解決方案?

回答

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文件內嵌。