Yii not executing my query in controller

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

you need to put the action you wish to perform for the selection in curly braces {}

eg:

if(true)

{

perform action,

perform another;

}

not

if(true)

perform action,

perform another

in your case, these lines


                $condition = "username = '".Yii::app()->user->name."'";

                $upd = Users::model('Users')->updateAll(array('last_login'=>$login_time), $condition);

will be executed regardless of whether $form->validate() returns true or false

What you should do in useridentity class is something like:




$user->saveAttributes(array('last_login'=>time()));



which saves you a few lines of code

that is:

$login_time = time();

$condition = “username = '”.strtolower($this->username)."’";

@jayrulez:

OT and really no offense, but could you avoid quoting such long messages completely? It makes threads sometimes hard to read and is usually considered … well … bad habit. ;)

Thanks jayrulez for the valuable advice. I switched to using saveAttributes().

…I am learning…

Cheers,

b