如果存在列,则跳过 Doctrine 迁移中的语句?

Pat*_*ick 0 php doctrine

我正在使用 Doctrine 迁移。我刚刚skipIf()AbstractMigration类中了解了该方法,如果存在列,我想用它来跳过一些代码。

有人可以提供一个我如何做到这一点的例子吗?

OK *_*ure 7

如果您的方法中有典型的Schema类,则可以执行以下操作:

public function up(Schema $schema)
{
    if (!$schema->getTable('your_table')->hasColumn('your_column')) {
        // do your code
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您想使用 ,skipIf您应该能够执行以下操作,尽管我尚未测试。

public function up(Schema $schema)
{
    $this->skipIf(
        $schema->getTable('your_table')->hasColumn('your_column'), 
        'Skipping because `your_column` exists'
    );
}
Run Code Online (Sandbox Code Playgroud)