致命错误:在第 189 行的 CModel.php 中找不到类“CModelEvent"
每当我尝试在不使用发布表单的情况下保存模型但直接从 excel 文件中获取数据并将其分配给模型属性时,我都会收到此错误(致命错误:未找到类CModelEvent"),如下所示:
I get this error(Fatal error: Class 'CModelEvent' not found) whenever I try to save model without using a post form but directly get data from an excel file and assign it to model attributes as given below:
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)
然后它保存记录但我想应用验证规则.如果有人知道这个错误,请帮助我......
I think this error is due to validation fails because when I try to save model as $model->save(false)
then It saves the record but i want to apply the validation rules.
Please help me if anybody knows about this error.....
如果我按如下所示更改上述功能,我还发现了另一件事:
I found one other thing if I cahnges my above function as given below:
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']
这会出错.
Then it saves the model but with uploading $_FILES['User'] ['tmp_name'['import_file']
this it gives error.
我通过在导入函数前添加 spl_autoload_unregister(array('YiiBase', 'autoload'));
解决了这个问题 Yii::import('application.extensions.yii-phpexcel.PHPExcel', true);
和 spl_autoload_register(array('YiiBase', 'autoload'));
之后.整个函数现在看起来像下面,现在工作正常:
I resolved this issue by adding spl_autoload_unregister(array('YiiBase', 'autoload'));
before import function Yii::import('application.extensions.yii-phpexcel.PHPExcel', true);
and spl_autoload_register(array('YiiBase', 'autoload'));
after it. The whole function looks like below now and works fine now:
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'));
}
}