Yii 即使存在所需的模型类,在创建模型的新实例时仍出现“找不到类”错误

Yii 即使存在所需的模型类,在创建模型的新实例时仍出现“找不到类”错误,yii,Yii,我有一个模型: class UserDetail extends CActiveRecord { public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return 'user_detail'; } } 当我这样做时: $userd

我有一个模型:

class UserDetail extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'user_detail';
    }
}
当我这样做时:

$userdetail = new UserDetail();
我得到一个错误: 致命错误:找不到类“UserDetail”,如果有人能帮我解决这个问题,请联系我。 我在行动中遇到以下问题:

public function actionImportFile() {
        if (isset($_POST['ImportFile'])) {
            Yii::import('application.extensions.yii-phpexcel.PHPExcel', true);
            $objPHPExcel = PHPExcel_IOFactory::load($_FILES['ImportFile']['tmp_name']['import_file']);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
            for ($row = 2; $row <= $highestRow; ++$row) {
                $model = new User('create');
                $model->firstname = $objWorksheet->getCellByColumnAndRow(1, $row)->getValue();
                $model->lastname = $objWorksheet->getCellByColumnAndRow(2, $row)->getValue();
                $model->email = $objWorksheet->getCellByColumnAndRow(3, $row)->getValue();
                $model->password = UserIdentity::encrypt($objWorksheet->getCellByColumnAndRow(4, $row)->getValue());
                $model->usertype = User:: getUserTypeCode($objWorksheet->getCellByColumnAndRow(5, $row)->getValue());
                $model->status = User:: getStatusCode($objWorksheet->getCellByColumnAndRow(6, $row)->getValue());
                $model->gender = User:: getGenderCode($objWorksheet->getCellByColumnAndRow(7, $row)->getValue());
                $model->current_grade = User::getGradeCode($objWorksheet->getCellByColumnAndRow(9, $row)->getValue());
                $model->createTime = time();
                if ($model->save(false)) {


                }
                $userdetail = new UserDetail();
                $userdetail->user_id = $model->id;
                $userdetail->birthday = strtotime($objWorksheet->getCellByColumnAndRow(8, $row)->getValue());
                $userdetail->zip = $objWorksheet->getCellByColumnAndRow(10, $row)->getValue();
                if ($userdetail->save(false)) {

                }
            }
        }
        $this->redirect(array('/admin/user/index'));
    }
公共函数actionImportFile(){
如果(isset($_POST['ImportFile'])){
Yii::import('application.extensions.Yii phpexcel.phpexcel',true);
$objPHPExcel=PHPExcel\u IOFactory::load($\u FILES['ImportFile']['tmp\u name']['import\u file']);
$objWorksheet=$objPHPExcel->getActiveSheet();
$highestRow=$objWorksheet->getHighestRow();
对于($row=2;$row firstname=$OBJWORK->GetCellByColumnRow(1,$row)->getValue();
$model->lastname=$objWorksheet->getCellByColumnRow(2,$row)->getValue();
$model->email=$objWorksheet->getCellByColumnRow(3,$row)->getValue();
$model->password=UserIdentity::encrypt($objWorksheet->getCellByColumnRow(4,$row)->getValue());
$model->usertype=User::getUserTypeCode($objWorksheet->getCellByColumnRow(5,$row)->getValue());
$model->status=User::getStatusCode($objWorksheet->getCellByColumnRow(6,$row)->getValue());
$model->gender=User::getGenderCode($objWorksheet->getCellByColumnRow(7,$row)->getValue());
$model->current_grade=User::getGradeCode($objWorksheet->getCellByColumnRow(9,$row)->getValue());
$model->createTime=time();
如果($model->save(false)){
}
$userdetail=newuserdetail();
$userdetail->user\u id=$model->id;
$userdetail->birthday=strottime($objWorksheet->getCellByColumnRow(8,$row)->getValue());
$userdetail->zip=$objWorksheet->getCellByColumnRow(10,$row)->getValue();
如果($userdetail->save(false)){
}
}
}
$this->redirect(数组('/admin/user/index'));
}
现在,当我改变上面的函数,如下所示,然后它工作良好,但请注意,我已经硬编码了一些行,我需要动态

 public function actionImportFile() {

        if (isset($_POST['ImportFile'])) {
            //Yii::import('application.extensions.yii-phpexcel.PHPExcel', true);
           // $objPHPExcel = PHPExcel_IOFactory::load($_FILES['ImportFile']['tmp_name']['import_file']);
           // $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = 2;//$objWorksheet->getHighestRow();
            for ($row = 2; $row <= $highestRow; ++$row) {
                $model = new User('create');
                $model->firstname ='test'; //$objWorksheet->getCellByColumnAndRow(1, $row)->getValue();
                $model->lastname ='test';  //$objWorksheet->getCellByColumnAndRow(2, $row)->getValue();
                $model->email = 'test@abc.com'; //$objWorksheet->getCellByColumnAndRow(3, $row)->getValue();
                $model->password = 'test';  //UserIdentity::encrypt($objWorksheet->getCellByColumnAndRow(4, $row)->getValue());
                $model->usertype = 1;//User:: getUserTypeCode($objWorksheet->getCellByColumnAndRow(5, $row)->getValue());
                $model->status = 1;//User:: getStatusCode($objWorksheet->getCellByColumnAndRow(6, $row)->getValue());
                $model->gender = 1;//User:: getGenderCode($objWorksheet->getCellByColumnAndRow(7, $row)->getValue());
                $model->current_grade = 1;//User::getGradeCode($objWorksheet->getCellByColumnAndRow(9, $row)->getValue());
                $model->createTime = time();
                if ($model->save(false)) {

                }
                $userdetail = new UserDetail();
                $userdetail->user_id = $model->id;
                $userdetail->birthday = '';//strtotime($objWorksheet->getCellByColumnAndRow(8, $row)->getValue());
                $userdetail->zip = 25000;//$objWorksheet->getCellByColumnAndRow(10, $row)->getValue();
                if ($userdetail->save(false)) {

                }
            }
        }
        $this->redirect(array('/admin/user/index'));
    }
公共函数actionImportFile(){
如果(isset($_POST['ImportFile'])){
//Yii::import('application.extensions.Yii phpexcel.phpexcel',true);
//$objPHPExcel=PHPExcel\u IOFactory::load($\u FILES['ImportFile']['tmp\u name']['import\u file']);
//$objWorksheet=$objPHPExcel->getActiveSheet();
$highestRow=2;//$objWorksheet->getHighestRow();
对于($row=2;$row firstname='test';//$objWorksheet->getCellByColumnRow(1,$row)->getValue();
$model->lastname='test';//$objWorksheet->getCellByColumnRow(2,$row)->getValue();
$model->email=test@abc.com“;//$objWorksheet->getCellByColumnRow(3,$row)->getValue();
$model->password='test';//UserIdentity::encrypt($objWorksheet->getCellByColumnRow(4,$row)->getValue());
$model->usertype=1;//User::getUserTypeCode($objWorksheet->getCellByColumnRow(5,$row)->getValue());
$model->status=1;//User::getStatusCode($objWorksheet->getCellByColumnRow(6,$row)->getValue());
$model->gender=1;//User::getGenderCode($objWorksheet->getCellByColumnRow(7,$row)->getValue());
$model->current_grade=1;//User::getGradeCode($objWorksheet->getCellByColumnRow(9,$row)->getValue());
$model->createTime=time();
如果($model->save(false)){
}
$userdetail=newuserdetail();
$userdetail->user\u id=$model->id;
$userdetail->birthday='';//strottime($objWorksheet->getCellByColumnRow(8,$row)->getValue());
$userdetail->zip=25000;//$objWorksheet->getCellByColumnRow(10,$row)->getValue();
如果($userdetail->save(false)){
}
}
}
$this->redirect(数组('/admin/user/index'));
}

我通过在导入函数
Yii::import('application.extensions.Yii phpexcel.phpexcel',true);
之前添加
spl_autoload_unregister(数组('YiiBase','autoload');
spl_autoload_寄存器(数组('YiiBase','autoload'))
之后。整个函数现在如下所示,并且可以正常工作:

public function actionImportFile() {
        if (isset($_POST['ImportFile'])) {
            spl_autoload_unregister(array('YiiBase', 'autoload'));
            Yii::import('application.extensions.yii-phpexcel.PHPExcel', true);
            spl_autoload_register(array('YiiBase', 'autoload'));
            $objPHPExcel = PHPExcel_IOFactory::load($_FILES['ImportFile']['tmp_name']['import_file']);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
            for ($row = 2; $row <= $highestRow; ++$row) {
                $model = new User('create');
                $model->firstname = $objWorksheet->getCellByColumnAndRow(1, $row)->getValue();
                $model->lastname = $objWorksheet->getCellByColumnAndRow(2, $row)->getValue();
                $model->email = $objWorksheet->getCellByColumnAndRow(3, $row)->getValue();
                $model->password = UserIdentity::encrypt($objWorksheet->getCellByColumnAndRow(4, $row)->getValue());
                $model->usertype = User:: getUserTypeCode($objWorksheet->getCellByColumnAndRow(5, $row)->getValue());
                $model->status = User:: getStatusCode($objWorksheet->getCellByColumnAndRow(6, $row)->getValue());
                $model->gender = User:: getGenderCode($objWorksheet->getCellByColumnAndRow(7, $row)->getValue());
                $model->current_grade = User::getGradeCode($objWorksheet->getCellByColumnAndRow(9, $row)->getValue());
                $model->createTime = time();
                if ($model->save(false)) {


                }
                $userdetail = new UserDetail();
                $userdetail->user_id = $model->id;
                $userdetail->birthday = strtotime($objWorksheet->getCellByColumnAndRow(8, $row)->getValue());
                $userdetail->zip = $objWorksheet->getCellByColumnAndRow(10, $row)->getValue();
                if ($userdetail->save(false)) {

                }
            }
        }
        $this->redirect(array('/admin/user/index'));
    }
公共函数actionImportFile(){
如果(isset($_POST['ImportFile'])){
spl_autoload_unregister(数组('YiiBase','autoload'));
Yii::import('application.extensions.Yii phpexcel.phpexcel',true);
spl_自动加载_寄存器(数组('YiiBase','autoload');
$objPHPExcel=PHPExcel\u IOFactory::load($\u FILES['ImportFile']['tmp\u name']['import\u file']);
$objWorksheet=$objPHPExcel->getActiveSheet();
$highestRow=$objWorksheet->getHighestRow();
对于($row=2;$row firstname=$OBJWORK->GetCellByColumnRow(1,$row)->getValue();
$model->lastname=$objWorksheet->getCellByColumnRow(2,$row)->getValue();
$model->email=$objWorksheet->getCellByColumnRow(3,$row)->getValue();
$model->password=UserIdentity::encrypt($objWorksheet->getCellByColumnRow(4,$row)->getValue());
$model->usertype=User::getUserTypeCode($objWorksheet->getCellByColumnRow(5,$row)->getValue());
$model->status=User::getStatusCode($objWorksheet->getCellByColumnRow(6,$row)->getValue());
$model->gender=User::getGenderCode($objWorksheet->getCellByColumnRow(7,$row)->getValue());
$model->current_grade=User::getGradeCode($objWorksheet->getCellByColumnRow(9,$row)->getValue());
$model->createTime=time();
如果($model->保存(false