关于Yii::$app->user->identity不能获取用户数据问题 [ 2.0 版本 ]
表单的数据是保存在User模型里,能实现登陆与退出功能.
这是动作方法
public function actionLogin()
{
$model=new User(['scenario'=>'login']);
if($model->load(Yii::$app->request->post())&&$model->login())
{
return $this->render('entry-confirm',['model'=>$model]);
}
else
{
return $this->render('login',['model'=>$model]);
}
}
然后在User实现login方法
public function login()
{
if(!$this->validate())
{
return false;
}
$user=Yii::$app->db->createCommand("select * from user where username=:username",[':username'=>$this->username])->queryOne();
if($user!==null)
{
if($this->checkPassword($user['password']))
{
return Yii::$app->user->login(User::findOne(['username'=>$this->username]),$this->rememberMe ? 3600*24*7:0);
}
return false;
}
return false;
}
这里还有个问题,如果这里用$user=User::fingOne(['username'=>$this->username])
获取对象
然后$user->password
为null,但是获取email其它的属性能获取到。
然后用Yii::$app->user->identity->username
获取数据,显示为空,var_dump(ii::$app->user->identity)
:
app\models\User Object
(
[username] =>
[password] =>
[rememberMe] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 6
[username] => raohong
[password] => $2y$13$4LMusps.IbhCfMBikgP6nuA.kmDmOyCqoKlbiOEv08lVhVZfiTbcy
[authkey] => -l_yYwqmHQPrrkmXSNR097TVV3s4r_VV
[accecctoken] =>
[email] => 724736528@qq.com
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 6
[username] => raohong
[password] => $2y$13$4LMusps.IbhCfMBikgP6nuA.kmDmOyCqoKlbiOEv08lVhVZfiTbcy
[authkey] => -l_yYwqmHQPrrkmXSNR097TVV3s4r_VV
[accecctoken] =>
[email] => 724736528@qq.com
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
有点奇怪,求解
37597 补充于 2016-07-11 23:50
问题解决了。
User模型不再获取表单信息,用了另外一个模型获取数据。
共 4 个回答
37597
注册时间:2016-10-31
最后登录:1970-01-01
在线时长:0小时0分
最后登录:1970-01-01
在线时长:0小时0分
- 粉丝2
- 金钱15
- 威望0
- 积分15