First: Use commands in your project
./yii Migrate/create Create_users_table
Generate the corresponding file to create the table in the Migrations directory in the project
<?php
Use Yii\db\schema;
Use yii\db\migration;
Use Yii\db\mysql;
Class M150225_074041_create_users_table extends migration
{
Public function up ()
{
$tableOptions = null;
if ($this->db->drivername== "MySQL") {
$tableOptions = "CHARACTER SET UTF8 COLLATE utf8_unicode_ci engine=innodb";
}
$this->createtable ("User", [
' ID ' =>schema::type_pk,
' username ' =>schema::type_string. " Not NULL ",
' Auth_key ' =>schema::type_string. " (+) Not NULL ",
' Display_name ' =>schema::type_string. " () Not NULL ",
"Password_hash" =>schema::type_string. " Not NULL ",
"Password_reset_token" =>schema::type_string,
"Email" =>schema::type_string,
"Role" =>schema::type_smallint. " Not NULL DEFAULT 10 ",
"Status" =>schema::type_smallint. " Not NULL DEFAULT 10 ",
"Created_at" =>schema::type_integer. " Not NULL ",
"Updated_at" =>schema::type_integer. " Not NULL ",
], $tableOptions);
}
Public function Down ()
{
}
}
Then run the command:./yii Migrate
The corresponding user table is generated in the database when the command is finished running.
The content in models/user.php is as follows:
<?php
namespace App\models;
Use Yii;
Use yii\base\notsupportedexception;
Use Yii\behaviors\timestampbehavior;
Use Yii\db\activerecord;
Use Yii\web\identityinterface;
/**
* The Model class for table "user".
*
* @property integer $id
* @property string $username
* @property string $auth _key
* @property string $display _name
* @property string $password _hash
* @property string $password _reset_token
* @property String $email
* @property integer $role
* @property integer $status
* @property integer $created _at
* @property integer $updated _at
*/
Class User extends ActiveRecord implements Identityinterface
{
/**
* @inheritdoc
*/
Const status_deleted=0;
Const STATUS_ACTIVE=10;
Const ROLE_USER=10;
public static function TableName ()
{
return ' user ';
}
Public Function behaviors () {
return [Timestampbehavior::classname (),];
}
/**
* @inheritdoc
*/
Public Function Rules ()
{
return [
[' Status ', ' Default ', ' Value ' =>self::status_active],
[' Status ', ' in ', ' range ' =>[self::status_active,self::status_deleted]],
[' Role ', ' Default ', ' Value ' =>self::role_user],
[' Role ', ' in ', ' range ' =>[self::role_user]],
];
}
public static function Findidentity ($id) {
return Static::findone ([' id ' = = $id, ' status ' =>self::status_active]);
}
public static function Findidentitybyaccesstoken ($token, $type =null) {
throw new NotSupportedException ("Findidentitybyaccesstoken is not implented.");
}
public static function Findbyusername ($username) {
return Static::findone ([' username ' = $username, ' status ' =>self::status_active]);
}
public static function Findbypasswordresettoken ($token) {
if (!static::ispasswordresettokenvalid ($token)) {
return null;
}
Return Static::findone ([
"Password_reset_token" = $token,
"Status" =>self::status_active,
]);
}
public static function Ispasswordresettokenvalid ($token) {
if (empty ($token)) {
return false;
}
$expire =yii:: $app->params[' User.passwordresettokenexpire ';
$parts = Explode ("_", $token);
$timestamp = (int) end ($parts);
return $timestamp + $expire >= time ();
}
Public Function getId () {
return $this->getprimarykey ();
}
Public Function Getauthkey () {
return $this->auth_key;
}
Public Function Validateauthkey ($authKey) {
return $this->getauthkey () = = = $authKey;
}
Public Function ValidatePassword ($password) {
Return Yii:: $app->security->validatepassword ($password, $this->password_hash);
return $this->password = = = $password;
}
Public Function SetPassword ($password) {
$this->password_hash=yii:: $app->security->generatepasswordhash ($password);
}
Public Function Generateauthkey () {
$this->auth_key=yii:: $app->security->generaterandomstring ();
}
Public Function Genratepasswordresettoken () {
$this->password_reset_token=yii:: $app->security->generaterandomstring (). " _ ". $time ();
}
Public Function Removepasswordresettoken () {
$this->password_reset_token=null;
}
// /**
* @inheritdoc
// */
Public Function Attributelabels ()
// {
return [
' id ' = ' id ',
' username ' = ' username ',
' Auth_key ' = ' auth key ',
' Display_name ' = ' Display name ',
' Password_hash ' = ' password hash ',
' Password_reset_token ' = ' password reset token ',
' Email ' = ' email ',
' Role ' = ' role ',
' Status ' = ' status ',
' Created_at ' = ' created at ',
' Updated_at ' = ' updated at ',
// ];
// }
}
User Login section in Yii2