我在使用 Symfony2 和 Doctrine 在非默认数据库上进行数据库迁移时遇到了一些麻烦。
我与两个数据库和两个实体管理器一起工作。我有两个包,每个包都与相应的 EM 一起使用,因此所有内容都应该完全分开。
在我的 DefaultBundle 下,我有大约 20 个迁移文件的迁移,它们都适用于默认数据库。我的 Secondbundle 有一个适用于 seconddb 的迁移文件。
当我尝试运行时
php app/console doctrine:migrations:migrate --no-interaction --em="second"
我得到了
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.
完全正确,“用户”是我主数据库中的一个表。这里发生的事情是它试图运行的迁移文件实际上是 DefaultCore 中 20 个中的第一个(它们都是旧的并且已经应用)。因为我已经指定 --em 它是我不想与我正在使用的数据库一起运行的交叉迁移。似乎没有另一个命令行选项来指定一个包,就像其他迁移命令(如生成)一样。我想忽略 DefaultBundle,只从我的 SecondBundle 运行我的迁移。
最佳答案
解决方案是您必须运行迁移两次,每个 --em 一次,然后在迁移本身中检查您在哪个数据库上,看看是否应该运行它。您进行迁移 ContainerAware,以便您有权执行此操作。示例:
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");
}
}
关于symfony - 多个数据库上的 Doctrine2 Symfony2 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26316440/