Model update problem

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)?




$user->save(false);



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)

Thanks, good to learn that :)

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

Thanks everyone now its working for me :)

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 :)

Thanks a lot it really cleared lot of my confusions :)