我发现 yii 可以通过以下方式获取并回显用户名:
echo Yii::app()->user->name;
我的问题是如何指定表用户中的字段与 CWebUser::name 绑定
我发现 yii 可以通过以下方式获取并回显用户名:
echo Yii::app()->user->name;
我的问题是如何指定表用户中的字段与 CWebUser::name 绑定
在你的配置中
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
'class'=>'WebUser',
),
WebUser 类(例如“user”表中的“email”字段):
class WebUser extends CWebUser
{
public function getEmail()
{
if(!$this->getState('__email')&&$this->id)
{
$user = User::model()->findByPk($this->id);
$this->setState('__email', $user->email);
}
$state = $this->getState('__email');
return $state;
}
public function login($identity, $duration=0)
{
$this->allowAutoLogin = true;
parent::login($identity, $duration);
$this->id = $identity->id;
if (!$this->isGuest)
{
if($user = User::model()->findByPk($this->id))
{
$this->setState('__email', $user->email);
}
}
}
}
您应该通过扩展 CUserIdentity 来编写自己的 UserIdentity 组件,并在用户标识后的 authenticate() 方法中声明 $this->username = $user->variable;