public function actionImport(){
$modelImport = new \yii\base\DynamicModel([
'fileImport'=>'File Import',
]);
$modelImport->addRule(['fileImport'],'required');
$modelImport->addRule(['fileImport'],'file',['extensions'=>'ods,xls,xlsx']);
if(Yii::$app->request->post()){
$modelImport->fileImport = \yii\web\UploadedFile::getInstance($modelImport,'fileImport');
if ($modelImport->fileImport && $modelImport->validate()){
$inputFileType = \PHPExcel_IOFactory::identify($modelImport->fileImport->tempName);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($modelImport->fileImport->tempName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$baseRow = 1;
while(!empty($sheetData[$baseRow]['A'])){
$model = new PersediaanBarang();
$model->rm_code = (string) $sheetData[$baseRow]['A'];
$model->quantity = (string) $sheetData[$baseRow]['B'];
$model->save();
$baseRow++;
}
Yii::$app->getSession()->setFlash('success','Success');
}else{
Yii::$app->getSession()->setFlash('error','Error');
}
//return $this->redirect(['barang/index']);
}
return $this->render('import',[
'modelImport'=>$modelImport,
]);
}

答案 0 :(得分:0)
SOLVE !!!!
<?php
namespace backend\controllers;
use Yii;
use backend\models\Penerimaan;
use backend\models\PenerimaanSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\Barang;
/**
* PenerimaanController implements the CRUD actions for Penerimaan model.
*/
class PenerimaanController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Penerimaan models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new PenerimaanSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Penerimaan model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
public function actionLihat()
{
$connection= \Yii::$app->db;
$users = $connection->createCommand('SELECT avg(price) as a FROM penerimaan')->queryAll();
var_dump($users);
}
/**
* Creates a new Penerimaan model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Penerimaan();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Penerimaan model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Penerimaan model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Penerimaan model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Penerimaan the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Penerimaan::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
public function actionImport(){
$model=new Penerimaan();
$modelImport = new \yii\base\DynamicModel([
'fileImport'=>'File Import',
]);
$modelImport->addRule(['fileImport'],'required');
$modelImport->addRule(['fileImport'],'file',['extensions'=>'ods,xls,xlsx']);
if(Yii::$app->request->post()){
$modelImport->fileImport = \yii\web\UploadedFile::getInstance($modelImport,'fileImport');
if ($modelImport->fileImport && $modelImport->validate()){
$inputFileType = \PHPExcel_IOFactory::identify($modelImport->fileImport->tempName);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($modelImport->fileImport->tempName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$baseRow = 1;
while(!empty($sheetData[$baseRow]['A'])){
$x = Barang::find()->where(['rm_code'=>$sheetData[$baseRow]['A']])->count();
if($x==0){
$connection = \Yii::$app->db;
$connection->createCommand()->insert('barang',['rm_code'=>$sheetData[$baseRow]['A']])->execute();
}
$model = new Penerimaan();
$model->rm_code = (string) $sheetData[$baseRow]['A'];
$model->quantity = (string) $sheetData[$baseRow]['B'];
$bulan = $_POST['Penerimaan']['bulan'];
$model->bulan= $bulan;
$model->save();
$baseRow++;
}
Yii::$app->getSession()->setFlash('success','Success');
}else{
Yii::$app->getSession()->setFlash('error','Error');
}
return $this->redirect(['penerimaan/index']);
}
return $this->render('import',[
'modelImport'=>$modelImport,
'model'=>$model,
]);
}
public function actionBarang()
{
$searchModel = new PenerimaanSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('barang', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
&#13;