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(){
$user = $this->loadUser(Yii::app()->user->username);
return $user->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(){
$user = $this->loadUser(Yii::app()->user->username);
return intval($user->userRole) == 1;
}
// Load user model.
protected function loadUser($id=null)
{
if($this->_model===null)
{
if($id!==null)
$this->_model=User::model()->findByPk($id);
}
return $this->_model;
}
public function afterLogout()
{
$user = $this->loadUser(Yii::app()->user->username);
$user->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