gpt4 book ai didi

php - Yii2 用户模型与关系表

转载 作者:可可西里 更新时间:2023-11-01 07:39:22 25 4
gpt4 key购买 nike

我是 Yii 的新手,我对它的用户和登录系统有疑问。我应该使用 3 个表进行登录检查,但是当我使用自定义查询时,我会遇到

“传递给 yii\web\User::login() 的参数 1 必须实现接口(interface) yii\web\IdentityInterface,给定 ActiveQuery”

我的 table 是这样的:

  • user : user_id, name, family, birthday, ...
  • 电子邮件:email_user_fk、email_addr、email_active、email_cdt
  • 密码:passwd_user_fk、passwd_hashed、passwd_active、passwd_cdt

我的查询是这样的:选择
user.user_id, email.email_addr, email.email_active,
passwd.passwd_hashed, passwd_passwd_active, ...
来自用户
在 user.user_id = email.email_user_fk 上加入电子邮件
JOIN passwd ON user.user_id = passwd.passwd_user_fk
在哪里
email.email_addr = :email

有什么想法吗??

class User extends ActiveRecord implements IdentityInterface
{
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;

/**
* @inheritdoc
*/
public static function tableName()
{
return 'user';
}

public static function primaryKey(){
return 'user_id';
}

/**
* @inheritdoc
*/
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}


/**
* @inheritdoc
*/
public static function find

Identity($id)
{
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}

/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}

/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::findOne(['email' => $username]);
}

public static function findByEmail($email)
{
return User::find()
->joinWith(['emails'])
->where("email.email_address = 'me@mail.com' ")
->one();
}

public static function findByMobile($email)
{
return User::find()
->joinWith(['mobiles'])
->where("mobile.mobile_address = '0931515124' ")
->one();
}


/**
* Finds user by password reset token
*
* @param string $token password reset token
* @return static|null
*/
public static function findByPasswordResetToken($token)
{
if (!static::isPasswordResetTokenValid($token)) {
return null;
}

return static::findOne([
'password_reset_token' => $token,
'status' => self::STATUS_ACTIVE,
]);
}

/**
* Finds out if password reset token is valid
*
* @param string $token password reset token
* @return boolean
*/
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();
}

/**
* @inheritdoc
*/
public function getId()
{
return $this->getPrimaryKey();
}

/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->auth_key;
}

/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}

/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password_hash);
}

/**
* 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();
}

/**
* Generates new password reset token
*/
public function generatePasswordResetToken()
{
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
}

/**
* Removes password reset token
*/
public function removePasswordResetToken()
{
$this->password_reset_token = null;
}

public function rules()
{
return [
[['user_name', 'user_family', 'user_birthday'], 'required'],
[['user_gender', 'city_id_fk', 'user_status'], 'integer'],
[['user_birthday', 'user_cdt'], 'safe'],
[['user_name'], 'string', 'max' => 32],
[['user_family'], 'string', 'max' => 48],
[['user_tel', 'user_postcode'], 'string', 'max' => 12],
[['user_address'], 'string', 'max' => 128],
[['user_profile_image', 'user_cover_image'], 'string', 'max' => 256]
];
}



/**
* @return \yii\db\ActiveQuery
*/
public function getEmails()
{
return $this->hasMany(Email::className(), ['email_user_id_fk' => 'user_id']);
}


/**
* @return \yii\db\ActiveQuery
*/
public function getMobiles()
{
return $this->hasMany(Mobile::className(), ['mobile_user_id_fk' => 'user_id']);
}


/**
* @return \yii\db\ActiveQuery
*/
public function getPasswds()
{
return $this->hasMany(Passwd::className(), ['passwd_user_id_fk' => 'user_id']);
}

}

最佳答案

这个错误表明当你执行 Yii::$app->user->login() 时,你必须传递一个实现身份接口(interface)的 User 对象作为参数(而且你似乎传递了另一个对象的类型)。

此方法的作用是允许您在登录后保存来自用户的信息。首先用户提供一个用户名,然后它必须从数据库中检索这个用户名的信息并用这个信息创建一个用户对象。这是您必须传递给 Yii::$app->user->login() 函数的对象。

阅读有关 User 类的信息 here .

并找到一个好的 sample here .

关于php - Yii2 用户模型与关系表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30278550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com