创建新的迁移
./yii migrate/create <name> //name参数会被用来生成迁移的类名的一部分(只包含字母、数字和下划线)
必填参数 name 的作用是对新的迁移做一个简要的描述。 例如,如果这个迁移是用来创建一个叫做 news 的表, 那么你可以使用 create_news_table 这个名称并运行如下命令:
yii migrate/create create_news_table
如上命令将会在 @app/migrations 目录下创建一个新的名为 m150101_185401_create_news_table.php 的 PHP 类文件。 该文件包含如下的代码,它们用来声明一个迁移类 m150101_185401_create_news_table, 并附有代码框架:
<?php
use yii\db\Migration;
class m150101_185401_create_news_table extends Migration
{
public function up()
{
}
public function down()
{
echo "m101129_185401_create_news_table cannot be reverted.\n";
return false;
}
/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}
每个数据库迁移文件的名称按照 m < YYMMDDHHMMSS >< Name > 的格式自动生成,其中
你可以指定更多的字段参数
yii migrate/create create_post --fields="title:string(12):notNull:unique,body:text"
生成
/**
* Handles the creation for table `post`.
*/
class m150811_220037_create_post extends Migration
{
/**
* @inheritdoc
*/
public function up()
{
$this->createTable('post', [
'id' => $this->primaryKey(),
'title' => $this->string(12)->notNull()->unique(),
'body' => $this->text()
]);
}
/**
* @inheritdoc
*/
public function down()
{
$this->dropTable('post');
}
}
主键会被自动添加同时默认名称为 id。 如果你想使用其他名称可以使用 --fields="name:primaryKey" 来指定名称。