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?
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.
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.
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);
}
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?