代码如下:
执行$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();
}
}