Php 我在yii中的登录模块没有';行不通

Php 我在yii中的登录模块没有';行不通,php,yii,Php,Yii,我正在yii框架中创建登录操作,但我有一个错误,无法修复。以下是错误: Property "AdminIdentity.user_name" is not defined. `$record=Admin::model()->findByAttributes(array('username'=>$this->user_name));` 这是我的登录模块: login.php <?php $form=$this->beginWidget('CActiveForm',

我正在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(); ?>  
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:

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
Model class:

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

类AdminIdentity扩展了CUserIdentity,在CUserIdentity中定义了两个属性:

  • $username
  • $password
  • 所以你要么改变

     $record=Admin::model()->findByAttributes(array('username'=>$this->user_name));
    


    在AdminIdentity类中声明$user\u name。如果选择声明新属性,请确保在构造函数中设置一个值(需要进行更多更改)

    declare
    user\u name
    after
    private$\u id是的,它可以工作,但在我的例子中它必须是这样的:$record=Admin::model()->findByAttributes(数组('user_name'=>$this->username)),多谢各位
    
     $record=Admin::model()->findByAttributes(array('username'=>$this->user_name));
    
      $record=Admin::model()->findByAttributes(array('username'=>$this->username));