Phinx 0.2.0 添加了一个称为可逆迁移的新功能。您可以使用方法实现可逆迁移change
。如果您已经实现了change
方法,那么您实际上并不需要编写down
或up
方法。在change
方法中向上迁移您的逻辑时,Phinx 将自动为您计算如何向下迁移。
当您定义表结构等时,可逆迁移很有帮助。
示例(使用更改方法的可逆迁移)
<?php
use Phinx\Migration\AbstractMigration;
class CreateUserLoginsTable extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
// create the table
$table = $this->table('user_logins');
$table->addColumn('user_id', 'integer')
->addColumn('created', 'datetime')
->create();
}
/**
* Migrate Up.
*/
public function up()
{
}
/**
* Migrate Down.
*/
public function down()
{
}
}
示例(与上述相同的迁移,不使用更改方法)
<?php
use Phinx\Migration\AbstractMigration;
class CreateUserLoginsTable extends AbstractMigration
{
/**
* Migrate Up.
*/
public function up()
{
// create the table
$table = $this->table('user_logins');
$table->addColumn('user_id', 'integer')
->addColumn('created', 'datetime')
->create();
}
/**
* Migrate Down.
*/
public function down()
{
$this->dropTable('user_logins');
}
}
阅读有关可逆迁移 Phinx的更多信息。