Example #1
0
 /**
  * Updates an existing Asset model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param string $id
  * @return mixed
  */
 public function actionUpdate($id = '')
 {
     $dates = $this->getDates($id);
     $model = Asset::find()->where(['TGL' => $dates[0]])->one();
     if (!$model) {
         $model = new Asset(['TGL' => $dates[0]]);
         if (!Yii::$app->request->post()) {
             foreach ($model->attributes as $key => $value) {
                 if ($key == 'TGL') {
                     $model->TGL = $dates[0];
                 } else {
                     $model->{$key} = 0.0;
                 }
             }
         }
         $model->save();
         $model->TGL = $dates[1];
     }
     $modelat = Assetat::find()->where(['TGL' => $dates[2]])->one();
     if (!$modelat) {
         $modelat = new Assetat(['TGL' => $dates[2]]);
         if (!Yii::$app->request->post()) {
             foreach ($modelat->attributes as $key => $value) {
                 if ($key != 'TGL') {
                     $modelat->{$key} = 0.0;
                 }
             }
         }
         $modelat->save();
         $modelat->TGL = $dates[3];
     }
     $searchModel = new IndikatorSearch(['TGL' => $dates[0]]);
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
     if ($model->load(Yii::$app->request->post()) and $modelat->load(Yii::$app->request->post())) {
         $connection = \Yii::$app->db;
         $transaction = $connection->beginTransaction();
         try {
             $dates = $this->getDates($model->TGL);
             $model->TGL = $dates[0];
             $modelat->TGL = $dates[2];
             if ($model->save() and $modelat->save()) {
                 Yii::$app->session->setFlash('success', 'Data berhasil disimpan.');
                 $transaction->commit();
             } else {
                 Yii::$app->session->setFlash('error', 'Data gagal disimpan.');
                 $transaction->rollBack();
             }
         } catch (Exception $e) {
             Yii::$app->session->setFlash('error', 'Fatal error.');
             $transaction->rollback();
         }
         return $this->redirect(['index', 'date' => $model->TGL]);
     } else {
         $model->TGL = $dates[1];
         //die($model->TGL);
         $modelat->TGL = $dates[3];
         return $this->render('update', ['model' => $model, 'modelat' => $modelat, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider]);
     }
 }