致命错误:Class' CModelEvent'在第189行的CModel.php中找不到

时间:2014-07-11 14:00:59

标签: php yii

每当我尝试在不使用帖子表单的情况下保存模型但直接从excel文件中获取数据并将其分配给模型属性时,我得到此错误(致命错误:类'CModelEvent'未找到),如下所示:

   public function actionImportFile() {
        if (isset($_POST['User'])) {
            Yii::import('application.extensions.yii-phpexcel.PHPExcel', true);
            $objPHPExcel = PHPExcel_IOFactory::load($_FILES['User']['tmp_name']['import_file']); 
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $model = new User('create');
            $model->firstname = $objWorksheet->getCellByColumnAndRow(1, 2)->getValue();
            $model->lastname = $objWorksheet->getCellByColumnAndRow(2, 2)->getValue();
            $model->password = 'password';
            $model->email = 'nt@yahoo.com';
            $model->usertype = -1;
            $model->status = 1;
            if ($model->save())
                $this->redirect(array('index'));
        }
    }

规则的定义如下:

  public function rules() {
        return array(
            array('firstname, lastname, email, status, usertype, password', 'required'),
        );
    }

我认为此错误是由于验证失败,因为当我尝试将模型保存为$model->save(false)时,它会保存记录但我想应用验证规则。 如果有人知道这个错误,请帮帮我.....

如果我改变我的上述功能,我发现了另外一件事:

public function actionImportFile() {
            if (isset($_POST['User'])) {
               // Yii::import('application.extensions.yii-phpexcel.PHPExcel', true);
               // $objPHPExcel = PHPExcel_IOFactory::load($_FILES['User'] ['tmp_name'['import_file']); 
              //  $objWorksheet = $objPHPExcel->getActiveSheet();
                $model = new User('create');
                $model->firstname = 'test'; //$objWorksheet->getCellByColumnAndRow(1, 2)->getValue();
                $model->lastname = 'test2';//$objWorksheet->getCellByColumnAndRow(2, 2)->getValue();
                $model->password = 'password';
                $model->email = 'nt@yahoo.com';
                $model->usertype = -1;
                $model->status = 1;
                if ($model->save())
                    $this->redirect(array('index'));
            }
        }

然后它会保存模型,但上传$_FILES['User'] ['tmp_name'['import_file']时会出错。

1 个答案:

答案 0 :(得分:0)

我通过在导入函数spl_autoload_unregister(array('YiiBase', 'autoload'));Yii::import('application.extensions.yii-phpexcel.PHPExcel', true);之后添加spl_autoload_register(array('YiiBase', 'autoload'));来解决此问题。整个功能现在看起来如下,现在工作正常:

 public function actionImportFile() {
        if (isset($_POST['User'])) {
            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['User']['tmp_name']['import_file']); 
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $model = new User('create');
            $model->firstname = $objWorksheet->getCellByColumnAndRow(1, 2)->getValue();
            $model->lastname = $objWorksheet->getCellByColumnAndRow(2, 2)->getValue();
            $model->password = 'password';
            $model->email = 'nt@yahoo.com';
            $model->usertype = -1;
            $model->status = 1;
            if ($model->save())
                $this->redirect(array('index'));
        }
    }