0

I am using to migrate in yii2 in command line

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

which create migration files but while using

yii migrate

enum doesn't work. I want to migrate without editing manually in m161215_121914_create_post_table.php. What should i do?

Bishwa Timilsina
  • 1,053
  • 9
  • 11
  • 2
    Possible duplicate of [How to make field enum migration yii2](http://stackoverflow.com/questions/39106804/how-to-make-field-enum-migration-yii2) – Salem Ouerdani Dec 15 '16 at 11:59
  • No i know that tricks which you want to say but i want to do migrate using command line without editing manually in m161215_121914_create_post_table.php int the above case. – Bishwa Timilsina Dec 16 '16 at 06:43
  • Hm, ColumnSchemaBuilder class don't have type "enum" in categoryMap - so it's probably not implemented. – TomaszKane Dec 16 '16 at 12:25

2 Answers2

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=可以'");
}
  • Although yii2 migrate can not directly support enum but can be converted through the original sql way to create, but also to achieve the goal – 小伙儿 Nov 30 '17 at 10:01
0

You could create ENUM in postgres like below

public function safeUp()
{
   $this->execute("CREATE TYPE statusEnum AS ENUM ('ACTIVE', 'INACTIVE')");

   $this->createTable('dummy_table', [
       "status0" => "statusEnum",
       "status1" => "statusEnum  default 'ACTIVE'",
       "status2" => $this->getDb()->getSchema()->createColumnSchemaBuilder("statusEnum  default 'ACTIVE'"),
   ];
}

public function safeDown()
{
    $this->execute('DROP TYPE statusEnum');
}
MjM
  • 571
  • 4
  • 15