简体   繁体   中英

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

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM