具有关系表的Yii2用户模型

时间:2022-11-24 20:20:13

I'm new to Yii and I have problem with its User and login system . I should use 3 tables for login check but when I use custom query I face

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

"Argument 1 passed to yii\web\User::login() must implement interface yii\web\IdentityInterface, ActiveQuery given"

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

my tables are like :

我的桌子就像:

  • user : user_id, name, family, birthday, ...
  • user:user_id,name,family,birthday,...

  • email : email_user_fk, email_addr, email_active, email_cdt
  • 电子邮件:email_user_fk,email_addr,email_active,email_cdt

  • passwd : passwd_user_fk, passwd_hashed, passwd_active, passwd_cdt
  • passwd:passwd_user_fk,passwd_hashed,passwd_active,passwd_cdt

and my query is like : SELECT user.user_id, email.email_addr, email.email_active, passwd.passwd_hashed, passwd_passwd_active , ... FROM user JOIN email ON user.user_id = email.email_user_fk JOIN passwd ON user.user_id = passwd.passwd_user_fk WHERE email.email_addr = :email

我的查询如下:SELECT user.user_id,email.email_addr,email.email_active,passwd.passwd_hashed,passwd_passwd_active,... FROM user JOIN email ON user.user_id = email.email_user_fk JOIN passwd ON user.user_id = passwd.passwd_user_fk在哪里email.email_addr =:电子邮件

Is there any Idea ,Please ??

请问有什么想法吗?

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']);
    }

}

1 个解决方案

#1


1  

What this error is showing is that when you execute Yii::$app->user->login() at some point you must pass a User object that implements identity interface as a parameter (and it seems you are passing another type of object).

这个错误显示的是,当你在某些时候执行Yii :: $ app-> user-> login()时,你必须传递一个实现身份接口的User对象作为参数(并且它似乎传递了另一种类型的对象)。

What this method does is allow you to save the information from the user after logging. First the user provides a username, then it has to retrieve info for this username from the database and instatiate a user object with this info. This is the object that you have to pass to the Yii::$app->user->login() function.

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

Read about the User class here.

在这里阅读User类。

and find a good sample here.

并在这里找到一个好样本。

#1


1  

What this error is showing is that when you execute Yii::$app->user->login() at some point you must pass a User object that implements identity interface as a parameter (and it seems you are passing another type of object).

这个错误显示的是,当你在某些时候执行Yii :: $ app-> user-> login()时,你必须传递一个实现身份接口的User对象作为参数(并且它似乎传递了另一种类型的对象)。

What this method does is allow you to save the information from the user after logging. First the user provides a username, then it has to retrieve info for this username from the database and instatiate a user object with this info. This is the object that you have to pass to the Yii::$app->user->login() function.

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

Read about the User class here.

在这里阅读User类。

and find a good sample here.

并在这里找到一个好样本。