/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionImport() { $model = new Dishes(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Dishes'])) { if (!empty($_FILES)) { $tempFile = $_FILES['Dishes']['tmp_name']['fileImport']; $fileTypes = array('xls', 'xlsx'); // File extensions $fileParts = pathinfo($_FILES['Dishes']['name']['fileImport']); if (in_array(@$fileParts['extension'], $fileTypes)) { Yii::import('ext.heart.excel.EHeartExcel', true); EHeartExcel::init(); $inputFileType = PHPExcel_IOFactory::identify($tempFile); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($tempFile); $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true); $baseRow = 2; $inserted = 0; $read_status = false; while (!empty($sheetData[$baseRow]['A'])) { $read_status = true; //$dish_id= $sheetData[$baseRow]['A']; $name = $sheetData[$baseRow]['B']; $model2 = new Dishes(); //$model2->dish_id= $dish_id; $model2->name = $name; try { if ($model2->save()) { $inserted++; } } catch (Exception $e) { Yii::app()->user->setFlash('error', "{$e->getMessage()}"); //$this->refresh(); } $baseRow++; } Yii::app()->user->setFlash('success', $inserted . ' row inserted'); } else { Yii::app()->user->setFlash('warning', 'Wrong file type (xlsx, xls, and ods only)'); } } $this->render('admin', array('model' => $model)); } else { $this->render('admin', array('model' => $model)); } }