Yii::app()->user->id 不返回空结果 [英] Yii::app()->user->id is not returning the empty result

查看:26
本文介绍了Yii::app()->user->id 不返回空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,可能是个小问题,但我找不到解决方案.Yii:app()->user->getId() 或 Yii:app()->user->id 没有返回值.它只是返回空结果.但是我已经在 UserIdentity 类中设置了 id.

I've a problem, it might be a small one but I couldn't find the solution for this. Yii:app()->user->getId() or Yii:app()->user->id is not returning value. it just returning empty result. But I've set the id in my UserIdentity class.

在我的 UserIdentity 类中,我有这个,

in my UserIdentity class I've this,

public function getId()
{
   return $this->id;
}

我正在 autheticate() 函数中设置 id.如果我在那里显示它本身,它会显示 id,但不在 getId() 函数中.请帮我解决这个问题.

I'm setting id in the autheticate() function. if i displayed there itself it's displaying the id, but not in getId() function. Please help me in this.

提前致谢.

达南德兰·拉贾戈帕尔.

Dhanendran Rajagopal.

推荐答案

不确定你是否使用标准的 UserIdentity 类,但在标准中 id 是一个私有属性,而在 Yii 约定中是私有的属性前面有 _ ,所以它会是这样的:

Not sure if you are using the standard UserIdentity class, but in the standard one id is a private attribute and in Yii convention private attributes have _ in front of them, so it would be like this:

public function getId()
{
    return $this->_id;
}

因为它是私有变量,所以你不能用 Yii::app()->user->_id 访问它.getId() 函数是一个神奇的函数,当你运行 Yii::app()->user->id 时,它看到没有 id 属性,因此查找名为 getId() 的函数并在它存在时运行它.

Since it is private variable you can't access it with Yii::app()->user->_id. The getId() function is a magic function, when you run Yii::app()->user->id, it sees there is no id attribute so looks for a function named getId() and runs it if it exists.

如果您使用的是标准类,则需要按照以下评论中的说明进行更改:

If you are using the standard class you need to change this as you stated in your comment below:

$this->_id = $user->user_id //underscore added before id

这篇关于Yii::app()->user->id 不返回空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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