属性 "CDbCriteria.username" 未被定义.

代码如下:

执行$user = User::model()->find(array(‘username’ => $this->username, ‘password’ => md5($this->password)));

抛出异常,在CDbCriteria类中 找不到 username 属性 ,请问是什么原因导致,谢谢

Controller:




<?php


class DefaultController extends Controller

{

    public $defaultAction = 'login';


    /**

     * 首页

     */

    public function actionIndex()

    {

        $this->render('index');

    }


    /**

     * 登录页

     */

    public function actionLogin()

    {

        if (isset($_POST['submit'])){

            $model = new User();

            $model->attributes = Yii::app()->request->getParam('user');

            if ($model->validate() && $model->login()) {

                $this->redirect(Yii::app()->createUrl('/passport/default/index'));

            }else{

                echo 'error';

            }

        }


        $this->render('login');

    }


}



MODEL:




<?php


/**

 * This is the MongoDB Document model class based on table "{{user}}".

 */

class User extends ActiveRecord

{

    public $username;

    public $password;

    private $__identity;

    

    public function tableName(){

        return '{{user}}';

    }


    /**

     * Returns the static model of the specified AR class.

     * @return User the static model class

     */

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }


    /**

     * returns the primary key field for this model

     */

    public function primaryKey()

    {

        return 'id';

    }


    /**

     * @return string the associated collection name

     */

    public function getCollectionName()

    {

        return 'bl_user';

    }


    /**

     * @return array validation rules for model attributes.

     */

    public function rules()

    {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

            array('username, password', 'required'),

            array('username', 'length', 'max'=>20),

            array('password', 'length', 'max'=>32),

            // The following rule is used by search().

            // Please remove those attributes that should not be searched.

            array('id, username, password', 'safe', 'on'=>'search'),

        );

    }


    /**

     * @return array customized attribute labels (name=>label)

     */

    public function attributeLabels()

    {

        return array(

            'username' => 'Username',

            'password' => 'Password',

        );

    }


    public function getDbComponent(){

        return 'mongodb';

    }


    /**

     * Logs in the user using the given username and password in the model.

     * @return boolean whether login is successful

     */

    public function login() {

        if ($this->__identity === null) {

            $this->__identity = new UserIdentity($this->username, $this->password);

            $this->__identity->authenticate();

        }

        if ($this->__identity->errorCode === UserIdentity::ERROR_NONE) {

            Yii::app()->user->login($this->__identity);

            return true;

        }else{

            return false;

        }

    }

}



UserIdentity.php:




<?php


/**

 * UserIdentity represents the data needed to identity a user.

 * It contains the authentication method that checks if the provided

 * data can identity the user.

 */

class UserIdentity extends CUserIdentity

{

    

    /**

     * Authenticates a user.

     * The example implementation makes sure if the username and password

     * are both 'demo'.

     * In practical applications, this should be changed to authenticate

     * against some persistent user identity storage (e.g. database).

     * @return boolean whether authentication succeeds.

     */

    public function authenticate()

    {

        $user = User::model()->find(array('username' => $this->username, 'password' => md5($this->password)));

        if(empty($user)){

            $this->errorCode = self::ERROR_USERNAME_INVALID;

        }else{

            $this->errorCode=self::ERROR_NONE;

        }

        return !$this->errorCode;

    }

    

}



ActiveRecord :




<?php


/**

 * 重写YII CActiveRecord  直接多DB 连接

 * @author shadow

 */

class ActiveRecord extends CActiveRecord 

{

    public static $database = array(); 

    

    public $dbname = 'db';

    

    /**

     * Returns the database connection used by active record.

     * By default, the "db" application component is used as the database connection.

     * You may override this method if you want to use a different database connection.

     * @return CDbConnection the database connection used by active record.

     */

    public function getDbConnection()

    {

        $dbname = $this->dbname;

        if(isset(self::$database[$dbname]) && self::$database[$dbname] instanceof CDbConnection){

            return self::$database[$dbname];

        }else{

            self::$database[$dbname] = Yii::app()->$dbname;

        }


        if(self::$database[$dbname] instanceof CDbConnection){

            self::$database[$dbname]->setActive(true); 

            return self::$database[$dbname];

        }else{

            throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));

        }

    }

    

    /**

     * 设置DB连接

     * @param string $dbname

     * @return CDbConnection the database connection used by active record.

     */

    public function setDbConnection($dbname){

        $this->dbname = $dbname;

        return $this->getDbConnection();

    }

}







CException


属性 "CDbCriteria.username" 未被定义.


E:\platform_jhs\trunk\framework\db\schema\CDbCriteria.php(153)


141      * </ul>

142      * @since 1.1.7

143      */

144     public $scopes;

145 

146     /**

147      * Constructor.

148      * @param array $data criteria initial property values (indexed by property name)

149      */

150     public function __construct($data=array())

151     {

152         foreach($data as $name=>$value)

153             $this->$name=$value;

154     }

155 

156     /**

157      * Remaps criteria parameters on unserialize to prevent name collisions.

158      * @since 1.1.9

159      */

160     public function __wakeup()

161     {

162         $map=array();

163         $params=array();

164         foreach($this->params as $name=>$value)

165         {

Stack Trace

#0	

+  E:\platform_jhs\trunk\framework\db\schema\CDbCriteria.php(153): CComponent->__set("username", "admin")

#1	

+  E:\platform_jhs\trunk\framework\db\schema\CDbCommandBuilder.php(462): CDbCriteria->__construct(array("username" => "admin", "password" => "21232f297a57a5a743894a0e4a801fc3"))

#2	

+  E:\platform_jhs\trunk\framework\db\ar\CActiveRecord.php(1391): CDbCommandBuilder->createCriteria(array("username" => "admin", "password" => "21232f297a57a5a743894a0e4a801fc3"), array())

#3	

–  E:\platform_jhs\trunk\protected\components\UserIdentity.php(21): CActiveRecord->find(array("username" => "admin", "password" => "21232f297a57a5a743894a0e4a801fc3"))

16      * against some persistent user identity storage (e.g. database).

17      * @return boolean whether authentication succeeds.

18      */

19     public function authenticate()

20     {

21         $user = User::model()->find(array('username' => $this->username, 'password' => md5($this->password)));

22         if(empty($user)){

23             $this->errorCode = self::ERROR_USERNAME_INVALID;

24         }else{

25             $this->errorCode=self::ERROR_NONE;

26         }

#4	

–  E:\platform_jhs\trunk\protected\modules\passport\models\User.php(80): UserIdentity->authenticate()

75      * @return boolean whether login is successful

76      */

77     public function login() {

78         if ($this->__identity === null) {

79             $this->__identity = new UserIdentity($this->username, $this->password);

80             $this->__identity->authenticate();

81         }

82         if ($this->__identity->errorCode === UserIdentity::ERROR_NONE) {

83             Yii::app()->user->login($this->__identity);

84             return true;

85         }else{

#5	

–  E:\platform_jhs\trunk\protected\modules\passport\controllers\DefaultController.php(23): User->login()

18     public function actionLogin()

19     {

20         if (isset($_POST['submit'])){

21             $model = new User();

22             $model->attributes = Yii::app()->request->getParam('user');

23             if ($model->validate() && $model->login()) {

24                 $this->redirect(Yii::app()->createUrl('/passport/default/index'));

25             }else{

26                 echo 'error';

27             }

28         }

#6	

+  E:\platform_jhs\trunk\framework\web\actions\CInlineAction.php(50): DefaultController->actionLogin()

#7	

+  E:\platform_jhs\trunk\framework\web\CController.php(309): CInlineAction->runWithParams(array())

#8	

+  E:\platform_jhs\trunk\framework\web\CController.php(287): CController->runAction(CInlineAction)

#9	

+  E:\platform_jhs\trunk\framework\web\CController.php(266): CController->runActionWithFilters(CInlineAction, array())

#10	

+  E:\platform_jhs\trunk\framework\web\CWebApplication.php(276): CController->run("")

#11	

+  E:\platform_jhs\trunk\framework\web\CWebApplication.php(135): CWebApplication->runController("passport")

#12	

+  E:\platform_jhs\trunk\framework\base\CApplication.php(162): CWebApplication->processRequest()

#13	

+  E:\platform_jhs\trunk\index.php(13): CApplication->run()