我在yii框架中创建登录操作,但是我有一个错误,我无法修复它。这是错误:
Property "AdminIdentity.user_name" is not defined. `$record=Admin::model()->findByAttributes(array('username'=>$this->user_name));`
这是我的登录模块:
login.php
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login_form',
'enableAjaxValidation'=>false,
'enableClientValidation' => true));
?>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div id="lower">
<?php echo $form->Checkbox($model,'rememberMe'); ?>
<?php echo $form->labelEx($model,'rememberMe'); ?>
<input type="submit" value="Login"/>
</div>
<?php $this->endWidget(); ?>
AdminController
和actionLogin
class AdminController extends Controller
{
public function actionLogin()
{
$this->layout = 'login';
$model=new LoginForm;
if(isset($_POST['ajax']) && $_POST['ajax']==='login_form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
if($model->validate() && $model->login()){
$this->redirect('/ktbeauty/index.php/categories/index');
}
}
$this->render('login',array(
'model'=>$model,
));
}
}
LoginForm
class LoginForm extends CFormModel{
public $username;
public $password;
public $rememberMe;
private $_identity;
public function rules()
{
return array(
// username and password are required
array('username, password', 'required','message'=>'input username requirement'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate','required','message'=>'input password requirement'),
);
}
public function attributeLabels()
{
return array(
'username'=>'User Name',
'rememberMe'=>'Remember me next time',
'password'=>'Password',
);
}
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new AdminIdentity($this->username,$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect username or password.');
}
}
public function login()
{
if($this->_identity===null)
{
$this->_identity=new AdminIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->admin->login($this->_identity,$duration);
return true;
}
else
return false;
}
}
AdminIdentity
上课:
class AdminIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=Admin::model()->findByAttributes(array('username'=>$this->user_name));
var_dump($record);exit;
if($record===null)
{
$this->_id='user Null';
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
else if($record->password!==$this->password)
{
$this->_id=$this->user_name;
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
else if($record['user_active']!=='1')
{
$err = "You have been Inactive by Admin.";
$this->errorCode = $err;
}
else
{
$this->_id=$record['ad_id'];
$this->setState('user_name', $record['user_name']);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
Admin
模特课:
class Admin extends CActiveRecord
{
public function tableName()
{
return 'admin';
}
public function rules()
{
return array(
array('user_status','length', 'max'=>1),
array('user_active','length', 'max'=>1),
array('user_name, password, email', 'length', 'max'=>100),
array('phone, cellphone, name', 'length', 'max'=>45),
array('ad_id, user_name, password, email, phone, cellphone, name, user_status, user_active', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
);
}
public function attributeLabels()
{
return array(
'ad_id' => 'Ad',
'user_name' => 'User Name',
'password' => 'Password',
'email' => 'Email',
'phone' => 'Phone',
'cellphone' => 'Cellphone',
'name' => 'Name',
'user_status' => 'User Status',
'user_active' => 'User Active',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('ad_id',$this->ad_id);
$criteria->compare('user_name',$this->user_name,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('phone',$this->phone,true);
$criteria->compare('cellphone',$this->cellphone,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('user_status',$this->user_status);
$criteria->compare('user_active',$this->user_active);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
答案 0 :(得分:2)
class AdminIdentity扩展了CUserIdentity,在CUserIdentity中定义了2个属性:
所以你应该改变
$record=Admin::model()->findByAttributes(array('username'=>$this->user_name));
到
$record=Admin::model()->findByAttributes(array('username'=>$this->username));
或
在AdminIdentity类中声明$ user_name。如果您选择声明新属性,请确保在构造函数中设置值(将需要更多更改)