问题
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'));
}
}
Rules are defined as given below:
public function rules() {
return array(
array('firstname, lastname, email, status, usertype, password', 'required'),
);
}
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'));
}
}
Then it saves the model but with uploading $_FILES['User'] ['tmp_name'['import_file']
this it gives error.
回答1:
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'));
}
}
来源:https://stackoverflow.com/questions/24699573/fatal-error-class-cmodelevent-not-found-in-cmodel-php-on-line-189