Question

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?

Was it helpful?

Solution 2

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));

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top