Php 如何指定用户表中要绑定到CWebUser的name属性的字段?
我发现yii可以通过以下方式获取并回显用户名:Php 如何指定用户表中要绑定到CWebUser的name属性的字段?,php,yii,yii-components,Php,Yii,Yii Components,我发现yii可以通过以下方式获取并回显用户名: echo Yii::app()->user->name; 我的问题是如何在表user中指定要与配置中的CWebUser::name绑定的字段 // application components 'components'=>array( 'user'=>array( // enable cookie-based authentication '
echo Yii::app()->user->name;
我的问题是如何在表user中指定要与配置中的CWebUser::name绑定的字段
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
'class'=>'WebUser',
),
WebUser类(表“用户”中“电子邮件”字段的示例):
您应该通过扩展CUserIdentity编写自己的UserIdentity组件,并在用户标识后的authenticate()方法中声明$this->username=$user->variable
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);
}
}
}
}