如何在Yii框架中获取CActiveRecord中的特定行?

时间:2013-12-05 12:00:26

标签: php activerecord yii cactiverecord

假设我有以下记录

用户     - ID     - 用户名      - 密码

登录时我想检查用户的x_username,带有此记录的x_password,我应该返回匹配的行。如何在CActiveRecord中做到这一点?

我尝试了以下

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

但它不起作用

怎么做?

2 个答案:

答案 0 :(得分:0)

yii框架中的登录管理。您应该使用UserIdentity组件。您需要修改组件文件夹下的用户身份,如下所示。

<?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

答案 1 :(得分:-1)

试试这个

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