首先:在项目中使用命令
./yii migrate/create create_users_table
在项目中的migrations目录中生成相应的创建表的文件
<?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."(32) NOT NULL",
'display_name'=>Schema::TYPE_STRING."(50) 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()
{
}
}
然后运行命令:./yii migrate
运行完命令后会在数据库中生成相应的user表。
在models/user.php中的内容如下:
<?php
namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
/**
* This is 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',
// ];
// }
}