/** * Creates a new Booking model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Booking(); $bookingField = new BookingFields(); $data = Yii::$app->request->post(); if ($model->load(Yii::$app->request->post())) { if ($model->save()) { if (isset($data['BookingFields']['fields'])) { $bookingField->fields = Json::encode($data['BookingFields']['fields']); $bookingField->booking_id = $model->id; $bookingField->save(); } return $this->redirect(['view', 'id' => $model->id]); } return $this->render('create', ['model' => $model]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Tour model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($id) { $tour = $this->findTour($id); $booking = new Booking(['tour_id' => $tour->id]); if ($booking->load(Yii::$app->request->post())) { $fields = ArrayHelper::map($tour->customFields, 'id', 'name'); $result = []; foreach ($_POST['CustomField'] as $key => $field_value) { if (!array_key_exists($key, $fields)) { continue; } $result[] = ['field' => $fields[$key], 'value' => $field_value, 'key' => $key]; } $booking->result = json_encode($result); if ($booking->save()) { \Yii::$app->getSession()->setFlash('success', 'Thanks for booking our tour. We will contact you soon.'); return $this->redirect(['tour/view', 'id' => $booking->tour_id]); } } return $this->render('create', ['tour' => $tour, 'booking' => $booking]); }