/**
  * 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]);
 }