Example #1
0
 public function actionIndexWithRelationships()
 {
     $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);
     $roomSelected = null;
     $reservationSelected = null;
     $customerSelected = null;
     if ($room_id != null) {
         $roomSelected = Room::findOne($room_id);
         $rooms = array($roomSelected);
         $reservations = $roomSelected->reservations;
         $customers = $roomSelected->customers;
     } elseif ($reservation_id != null) {
         $reservationSelected = Reservation::findOne($reservation_id);
         $rooms = array($reservationSelected->room);
         $reservations = array($reservationSelected);
         $customers = array($reservationSelected->customer);
     } elseif ($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('indexWithRelationShips', compact('roomSelected', 'reservationSelected', 'customerSelected', 'rooms', 'reservations', '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.');
     }
 }
 public function actionBook_detail($id)
 {
     $model = new Book();
     if (Yii::$app->user->isGuest) {
         return $this->redirect(Yii::$app->homeUrl . '?r=site/login');
     }
     if ($model->load(Yii::$app->request->post())) {
         $model->room_id = $id;
         $model->date_create = date('y-m-d', time());
         $room = Room::findOne($id);
         $totalPrice = (strtotime($model->date_to) - strtotime($model->date_from)) / (24 * 3600);
         $model->price = $totalPrice * $room->price;
         if ($model->save()) {
             $room->status = 1;
             $room->name = $model->name;
             $room->id_card = $model->id_card;
             $room->save();
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         return $this->render('book_detail', ['model' => $model]);
     }
 }