我有模特:
class UserDetail extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'user_detail';
}
}
当我这样做时:
$userdetail = new UserDetail();
我收到错误: 致命错误:Class' 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'));
}
现在当我改变上面给出的上述函数时,它工作正常,但请注意我已经硬编码了一些我需要动态的行。
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'));
}
答案 0 :(得分:1)
我通过在导入函数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['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'));
}