我发现yii可以得到并回显用户名:
echo Yii::app()->user->name;
我的问题是我如何指定表用户中的字段与CWebUser :: name
绑定答案 0 :(得分:2)
在您的配置中
// 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);
}
}
}
}
答案 1 :(得分:0)
您应该通过扩展CUserIdentity来编写自己的UserIdentity组件,并在用户标识声明$ this-> username = $ user-> variable;
之后在authenticate()方法中编写