错误:在非对象上调用成员函数validatePassword()(yii2)

时间:2015-05-22 07:18:33

标签: php yii2

我是这个框架中的新手,实际上这是我第一次使用框架。有人可以帮我解决这个错误我不知道如何解决这个错误。这是我的代码:

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;
    }
}

LoginForm.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代码是否正确?我真的不知道我的代码有问题可以有人解释我发生了什么吗?

3 个答案:

答案 0 :(得分:2)

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

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

    return $user;
}

答案 1 :(得分:0)

使用$user = new User(); $user = $this->getUser();,并在LoginForm.php的validatePassword()方法中将方法作为$ user-&gt; getUser()调用

答案 2 :(得分:0)

你在做 -

$user = $this->getUser();

if (!$user || !$user->validatePassword($this->password)) {

从< - p>设置$user

public function getUser()
{
    if ($this->_user === false) {
        $this->_user = User::findByUsername($this->username);
    }

    return $this->_user;
}

其中findByUsername()是类Model的方法。在这种情况下,$user不是User类的实例。

尝试 -

if (!$user || !User::validatePassword($this->password)) {