public function actionIndexWithRelationships() { // 1. Check what parameter of detail has been passed $room_id = Yii::$app->request->get('room_id', null); $reservation_id = Yii::$app->request->get('reservation_id', null); $customer_id = Yii::$app->request->get('customer_id', null); // 2. Fill three models: roomSelected, reservationSelected and customerSelected and // Fill three arrays of models: rooms, reservations and customer; $roomSelected = null; $reservationSelected = null; $customerSelected = null; if ($room_id != null) { $roomSelected = Room::findOne($room_id); $rooms = array($roomSelected); $reservations = $roomSelected->reservations; $customers = $roomSelected->customers; } else { if ($reservation_id != null) { $reservationSelected = Reservation::findOne($reservation_id); $rooms = array($reservationSelected->room); $reservations = array($reservationSelected); $customers = array($reservationSelected->customer); } else { if ($customer_id != null) { $customerSelected = Customer::findOne($customer_id); $rooms = $customerSelected->rooms; $reservations = $customerSelected->reservations; $customers = array($customerSelected); } else { $rooms = Room::find()->all(); $reservations = Reservation::find()->all(); $customers = Customer::find()->all(); } } } return $this->render('index-with-relationships', ['roomSelected' => $roomSelected, 'reservationSelected' => $reservationSelected, 'customerSelected' => $customerSelected, 'rooms' => $rooms, 'reservations' => $reservations, 'customers' => $customers]); }
/** * Finds the Room model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Room the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Room::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }