dongzhuang2030 2015-08-20 05:59
浏览 62
已采纳

Yii2:如何在对象上调用validatePassword()?

I am having a similar problem to this question on stackoverflow. The error I am receiving is

Call to a member function validatePassword() on a non-object

On my Users model I am using findOne() method to return the User object. When I try to validate user using this method on my LoginForm method I receive the error I have stated.

public function getUserStatus()
{
    if (!$this->hasErrors()) {
        $user = $this->getUser();

        if($user || $user->validatePassword($this->password)) {
            if($user->user_status == 0) {
                return 1;
            } else {
                return 8;
            }
        } else {
            return -1;
        }
    }
}

My getUser is the same as the one on Yii2 advanced template.

public function getUser()
{
    if ($this->_user === false) {
        $this->_user = Users::findByEmail($this->email, self::USER);
    }

    return $this->_user;
}

The problem I am facing is that when calling this there is no error:

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

This however has an error:

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

How is this as I believe it has got to do with PHP objects itself rather than the framework?

  • 写回答

1条回答 默认 最新

  • douhao6557 2015-08-20 06:07
    关注

    Check first if $user is not NULL. In your if statements you are facing PHP optimization (assuming $user is NULL:

    if(!$user || !$user->validatePassword($this->password))
    =
    if (true || unknown) // first argument is satisfied and no further actions are taken (second condition is ignored)
    

    if($user || $user->validatePassword($this->password))
    =
    if (false || unknown) // first argument is not satisfied, so PHP continues to check second parameter, thus leading to `NULL->validatePassword()`
    

    Same with if (false && someCondition) - optimization in effect.


    To fix that:

    if ($user && $user->validatePassword()) { // If first argument is `false`, don't evaluate second argument. If it's `true` - validate password
    

    or

    if ($user) {
        if ($user->validatePassword()) {
           // valid
        } else {
           // not valid
        }
    } else {
        //error
    }
    

    YOUR EXAMPLE

    public function getUserStatus()
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();
    
            if($user && $user->validatePassword($this->password)) {
                return $user->user_status == 0;
            }
        }
    
        return -1;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配