【问题标题】:I use setState in Yii but i also get Property "CWebUser.depId" is not defined我在 Yii 中使用 setState,但我也得到属性“CWebUser.depId”未定义
【发布时间】:2013-12-18 16:18:55
【问题描述】:

我想从表用户返回值 dep_id 以使用它,因此 setState 应该像 (Yii::app()->user->depId) 一样返回它,但是当我使用它时,我得到 - “CWebUser. depId”未定义。我搜索了,我不知道该怎么做,我需要一个快速的答案这是我的 示例:

private $_id;
//private $_dep_id;
public function authenticate()
{
    $user=User::model()->find('LOWER(username)=?',array(strtolower($this->username)));
    if($user===null)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    else if(!$user->validatePassword($this->password))
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
    {
        $this->_id=$user->id;
        $this->setState('depId',$user->dep_id);            
        $this->username=$user->username;
        $this->setState('lastLogin', date("m/d/y g:i A", strtotime($user->last_login_time)));
        $user->saveAttributes(array('last_login_time'=>date("Y-m-d H:i:s", time())));
        $this->errorCode=self::ERROR_NONE;
    }
    return $this->errorCode==self::ERROR_NONE;
}
public function getId()
{
    return $this->_id;
}

然后我得到了这个

Property "CWebUser.depId" is not defined.

这里有什么问题?!

【问题讨论】:

  • 调用Yii::app()->user->depId时用户是否经过身份验证,dep_id是正确的列名吗?
  • 同意@veelen 大概是如果 dep_id 是您应该使用的列名Yii::app()->user->dep_id
  • 如何确保用户已通过身份验证?!

标签: yii setstate


【解决方案1】:

您必须是经过身份验证的用户才能在上述场景中使用该变量,因为它仅限于经过身份验证的用户。

【讨论】:

  • 以及如何做到这一点?!
  • 我想您知道什么是经过身份验证的用户,因为您正在编写上面的代码来验证用户身份。您的应用程序用户将在使用 Web 应用程序所需的有效凭据登录后进行身份验证。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
相关资源
最近更新 更多