Yii Framework Forum: Yii not executing my query in controller - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii not executing my query in controller yii v 1.1 Rate Topic: -----

#1 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 04 January 2010 - 10:01 AM

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
0

#2 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 04 January 2010 - 10:56 AM

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)."'";

This post has been edited by jayrulez: 04 January 2010 - 12:06 PM

php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 04 January 2010 - 11:12 AM

@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. ;)
0

#4 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 05 January 2010 - 11:30 AM

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

...I am learning...

Cheers,
b
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users