2025-04-10 23:19:13 +08:00

105 lines
2.8 KiB
PHP
Executable File

<?php
namespace backend\models;
use Yii;
use backend\models\AuthAssignment;
/**
* This is the model class for table "c_user".
*
* @property integer $id
* @property string $username
* @property string $auth_key
* @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 \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%c_user}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['username','email'], 'required'],
//[['role', 'status', 'created_at', 'updated_at'], 'integer'],
[['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
[['auth_key'], 'string', 'max' => 32],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => Yii::t('common' ,'Username'),
'auth_key' => Yii::t('common' ,'Password'),
'password_hash' => 'Password Hash',
'password_reset_token' => 'Password Reset Token',
'email' => Yii::t('common' ,'Email'),
'mobile' => Yii::t('common' ,'Mobile'),
'role' => Yii::t('common' ,'Role'),
'status' => 'Status',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
//获取所有用户
public function get_all_user(){
$user = Yii::$app->db->createCommand('select * from '. tableName())->queryAll();
return $user;
}
public function getUsergrouplist()
{
/**
* 第一个参数为要关联的字表模型类名称,
*第二个参数指定 通过子表的 customer_id 去关联主表的 id 字段
*/
return $this->hasMany(AuthAssignment::className(), ['user_id' => 'id']);
}
public function getUsergroup()
{
/**
* 第一个参数为要关联的字表模型类名称,
*第二个参数指定 通过子表的 customer_id 去关联主表的 id 字段
*/
return $this->hasOne(AuthAssignment::className(), ['user_id' => 'id']);
}
/**
* Generates password hash from password and sets it to the model
*
* @param string $password
*/
public function setPassword($password)
{
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
/**
* Generates "remember me" authentication key
*/
public function generateAuthKey()
{
$this->auth_key = Yii::$app->security->generateRandomString();
}
}