2016-12-15 2 views
0

ich in yii2 in der BefehlszeileBefehlszeile Yii2 Migration für Enum

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

die Migration von Dateien erstellen zu migrieren verwenden aber während

yii migrate 

Enum mit nicht funktionieren. Ich möchte ohne manuelle Bearbeitung in m161215_121914_create_post_table.php migrieren. Was soll ich machen?

+2

Mögliche Duplikat (http://stackoverflow.com/questions/39106804/how-to-make-field-enum-migration-yii2) –

+0

Nein Ich weiß [Wie Feld Enum Migration yii2 machen], dass Tricks, die Sie sagen möchten, aber ich möchte migrieren über die Befehlszeile ohne manuelle Bearbeitung in m161215_121914_create_post_table.php int im obigen Fall. –

+0

Hm, ColumnSchemaBuilder-Klasse hat keinen Typ "enum" in categoryMap - also ist es wahrscheinlich nicht implementiert. – TomaszKane

Antwort

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=可以'"); 
} 
+0

Obwohl yii2 migrieren kann nicht direkt enum unterstützen, kann aber über die ursprüngliche sql-Methode zu erstellen, sondern auch um das Ziel zu erreichen –