/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = House::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'person_id' => $this->person_id]); $query->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; }
/** * Finds the House model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return House the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = House::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * 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]); }
public function find($id) { return House::find($id); }