Example #1
0
 public function actionUpdate($id)
 {
     $modelorder = $this->findModel($id);
     $modelsorders = $modelorder->table1s;
     if ($modelorder->load(Yii::$app->request->post())) {
         $oldIDs = ArrayHelper::map($modelsorders, 'id', 'id');
         $modelsorders = Model::createMultiple(Table1::classname(), $modelsorders);
         Model::loadMultiple($modelsorders, Yii::$app->request->post());
         $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsorders, 'id', 'id')));
         // ajax validation
         if (Yii::$app->request->isAjax) {
             Yii::$app->response->format = Response::FORMAT_JSON;
             return ArrayHelper::merge(ActiveForm::validateMultiple($modelsorders), ActiveForm::validate($modelorder));
         }
         // validate all models
         $valid = $modelorder->validate();
         $valid = Model::validateMultiple($modelsorders) && $valid;
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $modelorder->save(false)) {
                     if (!empty($deletedIDs)) {
                         Table1::deleteAll(['id' => $deletedIDs]);
                     }
                     foreach ($modelsorders as $modelTable1) {
                         $modelTable1->order_id = $modelorder->id;
                         if (!($flag = $modelTable1->save(false))) {
                             $transaction->rollBack();
                             break;
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['update', 'id' => $modelorder->id]);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     }
     return $this->render('update', ['modelorder' => $modelorder, 'modelsorders' => empty($modelsorders) ? [new Table1()] : $modelsorders]);
 }
 /**
  * Updates an existing Person model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $modelPerson = $this->findModel($id);
     $modelsHouse = $modelPerson->houses;
     $modelsRoom = [];
     $oldRooms = [];
     if (!empty($modelsHouse)) {
         foreach ($modelsHouse as $indexHouse => $modelHouse) {
             $rooms = $modelHouse->rooms;
             $modelsRoom[$indexHouse] = $rooms;
             $oldRooms = ArrayHelper::merge(ArrayHelper::index($rooms, 'id'), $oldRooms);
         }
     }
     if ($modelPerson->load(Yii::$app->request->post())) {
         // reset
         $modelsRoom = [];
         $oldHouseIDs = ArrayHelper::map($modelsHouse, 'id', 'id');
         $modelsHouse = Model::createMultiple(House::classname(), $modelsHouse);
         Model::loadMultiple($modelsHouse, Yii::$app->request->post());
         $deletedHouseIDs = array_diff($oldHouseIDs, array_filter(ArrayHelper::map($modelsHouse, 'id', 'id')));
         // validate person and houses models
         $valid = $modelPerson->validate();
         $valid = Model::validateMultiple($modelsHouse) && $valid;
         $roomsIDs = [];
         if (isset($_POST['Room'][0][0])) {
             foreach ($_POST['Room'] as $indexHouse => $rooms) {
                 $roomsIDs = ArrayHelper::merge($roomsIDs, array_filter(ArrayHelper::getColumn($rooms, 'id')));
                 foreach ($rooms as $indexRoom => $room) {
                     $data['Room'] = $room;
                     $modelRoom = isset($room['id']) && isset($oldRooms[$room['id']]) ? $oldRooms[$room['id']] : new Room();
                     $modelRoom->load($data);
                     $modelsRoom[$indexHouse][$indexRoom] = $modelRoom;
                     $valid = $modelRoom->validate();
                 }
             }
         }
         $oldRoomsIDs = ArrayHelper::getColumn($oldRooms, 'id');
         $deletedRoomsIDs = array_diff($oldRoomsIDs, $roomsIDs);
         if ($valid) {
             $transaction = Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $modelPerson->save(false)) {
                     if (!empty($deletedRoomsIDs)) {
                         Room::deleteAll(['id' => $deletedRoomsIDs]);
                     }
                     if (!empty($deletedHouseIDs)) {
                         House::deleteAll(['id' => $deletedHouseIDs]);
                     }
                     foreach ($modelsHouse as $indexHouse => $modelHouse) {
                         if ($flag === false) {
                             break;
                         }
                         $modelHouse->person_id = $modelPerson->id;
                         if (!($flag = $modelHouse->save(false))) {
                             break;
                         }
                         if (isset($modelsRoom[$indexHouse]) && is_array($modelsRoom[$indexHouse])) {
                             foreach ($modelsRoom[$indexHouse] as $indexRoom => $modelRoom) {
                                 $modelRoom->house_id = $modelHouse->id;
                                 if (!($flag = $modelRoom->save(false))) {
                                     break;
                                 }
                             }
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $modelPerson->id]);
                 } else {
                     $transaction->rollBack();
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     }
     return $this->render('update', ['modelPerson' => $modelPerson, 'modelsHouse' => empty($modelsHouse) ? [new House()] : $modelsHouse, 'modelsRoom' => empty($modelsRoom) ? [[new Room()]] : $modelsRoom]);
 }
Example #3
0
 /**
  * Update a model based on POST request.
  * @param Model $model
  * @return boolean returns true when model validated and saved.
  */
 public function updateModel($model)
 {
     if (Yii::$app->request->isPost) {
         if ($model->load(Yii::$app->request->post()) && $model->save()) {
             return true;
         }
     }
     return false;
 }