Skip to content

Instantly share code, notes, and snippets.

@dataskills
Created April 14, 2021 03:55
Yii2 Basic User Migration
<?php
use yii\db\Migration;
/**
* Class m200401_222729_user_table
*/
class m200401_222729_user_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
// user
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
}
$this->createTable('{{%user}}', [
'id' => $this->primaryKey(),
'username' => $this->string()->notNull()->unique(),
'auth_key' => $this->string(32)->notNull(),
'password_hash' => $this->string()->notNull(),
'password_reset_token' => $this->string()->unique(),
'email' => $this->string()->notNull()->unique(),
'status' => $this->smallInteger()->notNull()->defaultValue(10),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(),
], $tableOptions);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
// user
$this->dropTable('{{%user}}');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment