Hi,
Here is my weird experience with Yii today. I am developing an application using Yii on my localhost. I started installing some crap updates to my wamp server [windows] and screwed it up. I uploaded my application on my hosting and some things stopped working. One of them which I wasted a few hours to fix updates a field in my user table and more precisely updates the timestamp in a field representing a last login. Here is the query:
$login_time = time();
$condition = "username = '".Yii::app()->user->name."'";
$upd = Users::model('Users')->updateAll(array('last_login'=>$login_time), $condition);
Initially, the above query was located in my actionLogin:
public function actionLogin()
{
$form=new LoginForm;
// collect user input data
if(isset($_POST['LoginForm']))
{
$form->attributes=$_POST['LoginForm'];
// validate user input and redirect to previous page if valid
if($form->validate())
$login_time = time();
$condition = "username = '".Yii::app()->user->name."'";
$upd = Users::model('Users')->updateAll(array('last_login'=>$login_time), $condition);
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
$this->render('login',array('form'=>$form,'sql'=>$sql));
}
…no success.
I setup all types of logging and Yii never executed this as oppose to successfully running it on my localhost. I then copied the query and moved it in the UserIdentity.php class [the file is the same as in the blog tutorial]:
public function authenticate()
{
$username=strtolower($this->username);
$user=Users::model()->find('LOWER(username)=?',array($username));
if($user===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if(md5($this->password)!==$user->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else if($user->privilege == '0')
$this->errorCode=self::ERROR_INACTIVE;
else
{
$login_time = time();
$this->_id=$user->id;
$this->username=$user->username;
$condition = "username = '".strtolower($this->username)."'";
$upd = Users::model()->updateAll(array('last_login'=>$login_time), $condition);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
Can someone explain why this query would not run in my SiteController/login action but would successfully run in the UserIdentity class? I would like to know what I have missed to learn about MVC.
Thanks,
b