我试图获取用户ID,但到目前为止没有运气名称...Yii :: app() - > user-> id;得到的用户ID不
echo Yii::app()->user->id;
和
echo Yii::app()->user->getId();
回报用户名是奇怪的。 任何想法有什么不对?
我试图获取用户ID,但到目前为止没有运气名称...Yii :: app() - > user-> id;得到的用户ID不
echo Yii::app()->user->id;
和
echo Yii::app()->user->getId();
回报用户名是奇怪的。 任何想法有什么不对?
你应该的getId功能在用户身份
的Yii ::应用程序() - >用户默认返回组件CWebUser。
当你想获得一些关于用户的附加信息时,你需要扩展这个组件。
在您的components
文件夹中创建文件WebUser.php
。 (下面的示例所示)
class WebUser extends CWebUser {
/**
* Gets the FullName of user
*
* @return string
*/
public function getFullName()
{
return $this->_model->first_name . ' ' .$this->_model->last_name;
}
}
在您的配置文件中发现部分
'components'=>array(
'user'=>array(
'class'=>'WebUser'
)
)
如果没有这一部分,只是创建它。并将'class'=>更改为WebUser'。
使用的组件/的UserIdentity GETID功能得到用户的ID和名称等。
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$users=Users::model()->find("email_id='$this->username'");
$this->_id=$users->user_id;
}
public function getId(){
return $this->_id;
}
}
或者你可以使用setState
:
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=Users::model()->findByAttributes(array('mail'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password."325"))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id = $record->id;
$this->setState('role', $record->active);
$this->setState('mail', $record->mail);
$this->setState('name', $record->name);
$this->setState('surname', $record->surname);
$this->setState('mobile', $record->mobile);
$this->setState('adress', $record->adress);
$record->count_login += 1;
$record->update();
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
<?php echo Yii::app()->user->name." ".Yii::app()->user->surname; ?>
,如果你想显示它应该这样做的意见:
echo Yii::$app->user->id;
如上所述,您需要在您的身份类中实现getId()。它在这里解释:http://www.yiiframework.com/doc/guide/1.1/en/topics.auth#defining-identity-class – 2011-12-21 09:28:57