How to get a particular row in CActiveRecord in Yii framework?

1.7k views Asked by At

Lets say I have a record like the following

Users - id - username - password

While login i want to check user's x_username, x_password with this record and i should return the matched row. How to do that in CActiveRecord?

I tried the following

$user = Users::model()->find('username = :x_username AND password = :x_password');

but its not working

How to do this?

2

There are 2 answers

1
Let me see On BEST ANSWER

try this

 $user = Users::model()->find('username = :x_username AND password = 
:x_password',array(':x_username'=>'myname',':x_password'=>'mypassword'));

OR

   $user = Users::model()->find('username = :x_username AND password = 
    :x_password',array(':x_username'=>$myname,':x_password'=>$mypassword));
0
Elango Mani On

Login management in yii framework.You should use UserIdentity component .you need to modfify the useridentity under components folder as follows.

<?php


class UserIdentity extends CUserIdentity
{
    private $_id;

    public function authenticate()
    {

        $users=User::model()->find('LOWER(username)=?',array(strtolower($this->username)));
        if(!isset($users[$this->username]))
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        else if($users[$this->username]!==$this->password)
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else
        {
            $this->_id=$user->id;
            $this->username=$user->username;
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }
    public function getId()
    {
        return $this->_id;
    }
}

Refer this link for info