its.jawad
(Fastian060307)
July 5, 2012, 12:59pm
1
I am trying to update the lastLogin attribute of user model at the time of login but not sure why model save function not working.
$user = User::model()->findByPk(Yii::app()->user->id);
$user->lastLogin = date(‘Y-m-d H:i:s’);
$user->save();
(I am doing is in site controller login function)
I know there might be an issue of model rules but what if I want to update a single field of a table bypassing all validation rules (only in this case)?
Bianca
(Biancajsen)
July 5, 2012, 1:20pm
3
its.jawad:
I am trying to update the lastLogin attribute of user model at the time of login but not sure why model save function not working.
$user = User::model()->findByPk(Yii::app()->user->id);
$user->lastLogin = date(‘Y-m-d H:i:s’);
$user->save();
(I am doing is in site controller login function)
I know there might be an issue of model rules but what if I want to update a single field of a table bypassing all validation rules (only in this case)?
I think you should then use $user->save(false)
also, take a look at CWebUser::afterLogin -> http://www.yiiframework.com/doc/api/1.1/CWebUser#afterLogin-detail
this is the place where you should place your code to update the last login (if you use Yii’s authentication way)
Bianca
(Biancajsen)
July 5, 2012, 1:24pm
5
Thanks, good to learn that
mdomba
(Maurizio Domba Cerin)
July 5, 2012, 1:38pm
6
If you want to save only one or just few attributes you can use saveAttributes() - http://www.yiiframework.com/doc/api/1.1/CActiveRecord#saveAttributes-detail
its.jawad
(Fastian060307)
July 9, 2012, 12:34pm
7
Thanks everyone now its working for me
its.jawad
(Fastian060307)
July 9, 2012, 1:02pm
8
Thanks for this useful info but can you please guide me where should I write that afterLogin function ?
CWebUser is the class you access when you do Yii::app()->user;
You can create a new class, say WebUser which extends CWebUser and place that class in the /components directory.
Then, in that class, add the afterLogin() method.
Finally, in your main config file, in the user section, add the WebUser class instead og CWebUser
These are big lines that should help you get going, search the form about this topic, it has been discusses several times already
its.jawad
(Fastian060307)
July 10, 2012, 10:30am
10
twisted1919:
CWebUser is the class you access when you do Yii::app()->user;
You can create a new class, say WebUser which extends CWebUser and place that class in the /components directory.
Then, in that class, add the afterLogin() method.
Finally, in your main config file, in the user section, add the WebUser class instead og CWebUser
These are big lines that should help you get going, search the form about this topic, it has been discusses several times already
Thanks a lot it really cleared lot of my confusions