错误:在非对象 (yii2) 上调用成员函数 validatePassword() [英] error: Call to a member function validatePassword() on a non-object (yii2)

查看:23
本文介绍了错误:在非对象 (yii2) 上调用成员函数 validatePassword()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个框架的新手,实际上这是我第一次使用框架.有人能帮我解决这个错误吗?我不知道如何修复这个错误.这是我的代码:

I'm new in this framework actually this is my first time to use framework. Can someone help me on this error I don't know how to fixed this error. here is my code:

User.php:

<?php

namespace app\models;
use Yii;
class User extends  \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
    public $id;
    public $username;
    public $password;
    public $authKey;
    public $accessToken;

   public static function tableName()
    {
        return 'user_file';
    }


    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        foreach (self::$users as $user) {
            if ($user['accessToken'] === $token) {
                return new static($user);
            }
        }

        return null;
    }

    /**
     * Finds user by username
     *
     * @param  string      $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        $user = User::find()
        ->where(['usr_id' => $username])
    ->all();

        return $user;
    }

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

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

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

    /**
     * Validates password
     *
     * @param  string  $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {

        return $this->password === $password;
    }
}

登录表单.php

<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * LoginForm is the model behind the login form.
 */
class LoginForm extends Model
{
    public $username;
    public $password;
    public $rememberMe = true;

    private $_user = false;


    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();

            if (!$user || !$user->validatePassword($this->password)) {
                $this->addError($attribute, 'Incorrect username or password.');
            }
        }
    }

    /**
     * Logs in a user using the provided username and password.
     * @return boolean whether the user is logged in successfully
     */
    public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
        } else {
            return false;
        }
    }

    /**
     * Finds user by [[username]]
     *
     * @return User|null
     */
    public function getUser()
    {
        if ($this->_user === false) {
            $this->_user = User::findByUsername($this->username);
        }

        return $this->_user;
    }
}

我的 User.php 代码是否正确?我真的不知道我的代码有问题,有人可以解释一下发生了什么吗?

Is my User.php code is correct? I really don't know the problem with my code can someone explain me what's happening?

推荐答案

根据 http://www.yiiframework.com/doc-2.0/yii-db-activequery.html#all()-detail all() 返回一个数组.在 findByUsername() 中,您将返回此数组.这就是为什么您会调用缺少成员函数错误的原因,因为数组不是对象.尝试返回 ->one() 而不是 all() 像:

According to http://www.yiiframework.com/doc-2.0/yii-db-activequery.html#all()-detail all() returns an array. In findByUsername() you are returning this array. This is why you getting the call to missing member function error since an array is not an object. Try returning ->one() instead of all() like:

public static function findByUsername($username)
{
    $user = User::find()
    ->where(['usr_id' => $username])
->one();

    return $user;
}

这篇关于错误:在非对象 (yii2) 上调用成员函数 validatePassword()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆