public function actionImport() { if (isset($_POST['media'])) { $arr = preg_split('/[,]/', $_POST['media']); $file_path = Yii::getPathOfAlias('webroot') . '/uploads/' . $arr[0]; $sheet_array = Yii::app()->yexcel->readActiveSheet($file_path); $countError = 0; $rowError = ''; $index = 1; foreach ($sheet_array as $row) { if ($index > 1) { try { $code = trim($sheet_array[$index]['B']); $model = Product::model()->findByAttributes(array('code' => $code)); if ($model === null) { $model = new Product(); } $model->name = trim($sheet_array[$index]['A']); $model->non_utf8_name = Common::removeUnicodeURL($model->name); if ($code == '') { $model->code = 'SP_'; } else { $model->code = $code; } $model->model = trim($sheet_array[$index]['C']); $model->color = trim($sheet_array[$index]['D']); $model->origin = trim($sheet_array[$index]['E']); $model->unit = trim($sheet_array[$index]['F']); $model->price = trim($sheet_array[$index]['G']); $model->discount = trim($sheet_array[$index]['H']); $model->summary = trim($sheet_array[$index]['I']); $strCategory = trim($sheet_array[$index]['J']); $strCategory = Common::removeUnicodeURL($strCategory); $category = ProductCategories::model()->findByAttributes(array('non_utf8_name' => $strCategory)); if ($category != null) { $model->productCategories = $category->id; } $model->description = trim($sheet_array[$index]['K']); $model->order = trim($sheet_array[$index]['L']); $media = trim($sheet_array[$index]['M']); if ($media == '') { $media = '-'; } $model->createdby = Yii::app()->user->id; $model->createdatetime = new CDbExpression('NOW()'); $model->setRelationRecords('media', $this->getRelationMedia('tbl_product', $media)); if (!$model->save()) { $rowError = $rowError . $index . ','; $countError += 1; } else { if ($code == '') { $model->code = sprintf('SP_%1$06d', $model->id); $model->save(); } } } catch (Exception $e) { //throw new CHttpException(500,$e->getMessage()); $rowError = $rowError . $index . ','; $countError += 1; } } $index += 1; } if ($countError == 0) { Yii::app()->user->setFlash('importdata', 'Quá trình nhập liệu thành công!'); } else { Yii::app()->user->setFlash('importdata', 'Có ' . $countError . ' dòng (' . substr($rowError, 0, -1) . ') trong ' . (count($sheet_array) - 1) . ' dòng dữ liệu xảy ra lỗi trong quá trình nhập liệu!'); } $this->refresh(); } $this->render('import'); }