User logoutTime

Hello everyone

How I can save last user’s logout Date Time into the database?

To save the login Date Time:

Inside UserIdentity.php and inside authenticate function I added these lines after everything is OK:

public function authenticate()

{

else

	{


		$this->errorCode=self::ERROR_NONE;


		$user->logInDateTime= new CDbExpression('NOW()');


		$user->saveAttributes(array('logInDateTime'));


	}

}

I was reading other ppl’s posts, they were saying to override afterLogout() in some something like WebUser.php

That’s what I have inside WebUser.php

<?php

// this file must be stored in:

// protected/components/WebUser.php

class WebUser extends CWebUser {

// Store model to not repeat query.

private $_model;

// Return first name.

// access it by Yii::app()->user->first_name

function getUser_Name(){

&#036;user = &#036;this-&gt;loadUser(Yii::app()-&gt;user-&gt;username);


return &#036;user-&gt;username;

}

// This is a function that checks the field ‘role’

// in the User model to be equal to 1, that means it’s admin

// access it by Yii::app()->user->isAdmin()

function isAdmin(){

&#036;user = &#036;this-&gt;loadUser(Yii::app()-&gt;user-&gt;username);


return intval(&#036;user-&gt;userRole) == 1;

}

// Load user model.

protected function loadUser($id=null)

{


    if(&#036;this-&gt;_model===null)


    {


        if(&#036;id&#33;==null)


            &#036;this-&gt;_model=User::model()-&gt;findByPk(&#036;id);


    }


    return &#036;this-&gt;_model;


}





public function afterLogout()





{


	&#036;user = &#036;this-&gt;loadUser(Yii::app()-&gt;user-&gt;username);


	&#036;user-&gt;logOutDateTime='fff'; //For now I have set logOutDateTime data type to varChar(45) just


                                         //to test it actually saves anything or not. 


                                         //Later on I will change it DateTime and use NOW()


                                         //as I did for log in


}

}

?>

but it does nothing. Dont know how to use the WebUser.php and perhaps how/where. I have it in protected/components but dont know how to use/call it !!

Thanks

you dont call $user->save() in afterLogout()?

My WebUser now has only one function as follow:

<?php

// this file must be stored in:

// protected/components/WebUser.php

class WebUser extends CWebUser {

public function afterLogout()





{


	&#036;user=user::Model();


	&#036;user-&gt;logOutDateTime='TEST';


	&#036;user-&gt;saveAttributes(array('logOutDateTime'));


	parent::afterLogout();  


}

}

?>

and the main.php has these line, too:

// application components


'components'=&gt;array(


	'user'=&gt;array(


		// enable cookie-based authentication


			'class'=&gt;'WebUser',


			'allowAutoLogin'=&gt;true,


		


	)

but still does not save anything.

Where I went wrong?

Please, use [ code ][ /code ] tags, otherwise no one will read your code :)

in afterLogout Yii::app()->user is already a Guest, so you have to get a users ID from somewhere else, maybe beforeLogout()