枚举命令行Yii2迁移

时间:2016-12-15 11:41:29

标签: enums yii2 command migration

我正在使用命令行中的yii2进行迁移

yii migrate/create create_post_table --fields="customer_id:integer,registered_from:enum('app','web'),created_at:datetime,updated_at:datetime"

创建迁移文件但使用

yii migrate

enum不起作用。  我想在m161215_121914_create_post_table.php中手动编辑而不进行手动编辑。我该怎么办?

1 个答案:

答案 0 :(得分:0)

public $tableName = 'sms_template';

public function up()
{
    $result = $this->db->createCommand('SELECT table_name FROM information_schema.TABLES WHERE table_name ="' . $this->tableName . '"')->queryAll();
    if ($result) {
        return true;
    }

    $tableOptions = null;
    if ($this->db->driverName === 'mysql') {
        $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=MyISAM';
    }

    $this->createTable(
        $this->tableName,
        [
            'id' => $this->integer(5)->unsigned()->notNull()->comment('模板id'),
            'name' => $this->string(32)->notNull()->defaultValue('0')->comment('模板名字')
        ],
        $tableOptions
    );
    $this->addPrimaryKey('id', $this->tableName, 'id');

    //处理枚举类型字段 enum
    $this->addColumn($this->tableName, 'status', "enum('0','1') COLLATE utf8_unicode_ci DEFAULT '0' COMMENT '模板状态 0=失败,1=成功'");
    $this->addColumn($this->tableName, 'is_delete', "enum('0','1') COLLATE utf8_unicode_ci DEFAULT '0' COMMENT '是否可删除 0=不可以 1=可以'");
}