Updating User on Login

Hi,

I'm trying to figure out the best way to update 2 fields in a user table after successful login. I want to store the current Date time of the login and set a isLoggedIn bit to show users that are logged into the site.

Should I add a new method to the SiteController to do this or should it be in the UserIdentity component?

Also, can I just call the save() method to update or is there a better way to handle updates to certain fields?

Thank you,

Ben

I think I would extend CWebUser::login().  You can update/save as you normallu would (User::model()->save())

where would I extend CWebUser? I wouldn't think I would have to create a new class to do a simple update like that.

The SiteController already has the actionLogin() function, you don't think it makes sense to just call the update after the user has been validated in the actionLogin()?

I just started with Yii today and I still don't know PHP all that well so I'm trying to get my head around this.

Ben

you could do within actionLogin(), but in some far-away situation you may want to login a user from another location.  It is likely you may want to extend CWebUser in other ways too in the future, so I see no problem in starting now.  This is more bullet-proof.

What I did is I created a new class WebUser (drop the 'C'), put in in protected/components, and update the config file to use it. 

<?php





	'user'=>array(


		//'class'=>'application.components.CWebUser', //old


		'class'=>'application.components.WebUser', //new


		//...


	),


You could also extend CWebUser::logout() to disable the isLoggedIn bit if you like

I guess I'm not getting what you're saying. If I create a WebUser Class to extend CWebUser, how will that be called?

Do you have an example of once I log in I can use that class to update the 2 fields I need to update?

just make the modification in the main.php config file as I showed, and Yii will take care of the rest.  You still call it as you did before, eg Yii::app()->user->login(…)

this is some basic code of WebUser you could have:

<?php





public function login($identity,$duration=0) {





	parent::login($identity,$duration)





	$user = User::model()->find('where id='.$identity->id);


	//modify $user attributes here


	$user->save(false);





}


not tested or anything, just a basic idea

okay, I get what you're saying.

One thing I'm not sure on because I've never used Active Record or scaffolding before is how can I update just a single field using the save() method?

I see in the docs that there are also some update() methods but I don't see any real world examples on updating a single field. Let's say I want to set a bit field to a 1 or a 0. Do you know how that is done?

Nevermind, I see how to do it now.

Just had to get the current user and then set the fields to the values I want and save.

For those that might be new to Yii and trying to figure out how to do this, here is the code I used:

$users=users::model()->findByPk(Yii::app()->user->id);

$users->isUserLoggedIn=1;

$users->last_login_date = new CDbExp​ression('NOW()');

$users->save();

Thank you very much for your help Jonah.

I'm sure I'll have a ton more questions as I get deeper into this app.

Ben