Пример #1
0
 /**
  * Updates an existing Reserva 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);
     $modelHora = $model->horaMedica;
     $modelsExamen = $modelHora->horaExamenSolicitados;
     if ($model->load(Yii::$app->request->post()) && $modelHora->load(Yii::$app->request->post())) {
         $oldIDHora = ArrayHelper::map($modelHora, 'id', 'id');
         $modelHora = new Hora();
         Model::load($modelHora, Yii::$app->request->post());
         $deletedIDHora = array_diff($oldIDHora, array_filter(ArrayHelper::map($modelHora, 'id', 'id')));
         $oldIDsExamen = ArrayHelper::map($modelsExamen, 'id', 'id');
         $modelsExamen = Model::createMultiple(Pago::classname(), $modelsExamen);
         Model::loadMultiple($modelsExamen, Yii::$app->request->post());
         $deletedIDsExamen = array_diff($oldIDsExamen, array_filter(ArrayHelper::map($modelsExamen, 'id', 'id')));
         $valid = $model->validate();
         $valid = $modelHora->validate();
         $valid = Model::validateMultiple($modelsExamen) && $valid;
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     if (!empty($deletedIDHora)) {
                         Hora::deleteAll(['id' => $deletedIDHora]);
                     } else {
                         if (!empty($deletedIDsExamen)) {
                             HoraExamenSolicitado::deleteAll(['id' => $deletedIDsExamen]);
                         }
                     }
                     foreach ($modelsExamen as $modelExamen) {
                         $modelExamen->hora_id = $model->hora_medica_id;
                         if (!($flag = $modelExamen->save(false))) {
                             $transaction->rollBack();
                             break;
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     }
     return $this->render('update', ['model' => $model, 'modelHora' => $modelHora, 'modelsExamen' => empty($modelsExamen) ? [new HoraExamenSolicitado()] : $modelsExamen]);
 }