how to update a field in user table after login

hi i m trying to update the value of the Last_visited in the user table i m currently using this in the site controller


private function lastActive(){

       

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

        $user_model->last_active = date('Y-m-d H:i:s');

        if($user_model->save()){

            print_r($user_model->last_active);

        }else{

            echo 'hello it is not working';

        }



and calling it in the actionLogin function




if($model->validate() && $model->login()){

		$this->lastActive();

                Yii::app()->end();

			     

                 if(!Yii::app()->user->isGuest){

			         $this->redirect(array('user/home','id'=>Yii::app()->user->id));

                     

			     }else{

			         $this->redirect(Yii::app()->user->returnUrl);

			     }

                 

			}



but it is not working any idea?

If save() returns "false" then there are probably some validation errors:




$user_model->getErrors();



what type is "last_active" in the database?

date returns a formatted string.

the type of last_active is date time

actually when the model was being saved it required some more attributes which were defined required in the rules so now i m using this to accomplish the issue in the loginForm Model’s login function




if($this->_identity->errorCode===UserIdentity::ERROR_NONE)

		{

			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days

			Yii::app()->user->login($this->_identity,$duration);

            User::model()->updateByPk($this->_identity->id,array('last_active'=> new CDbExpression('NOW()')));

			return true;

		}



Since you need to save a single field and don’t need a validation, it’s the best approach.