/** * Creates a new Booths model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Booths(); $modelsBoothSchedules = [new BoothSchedules()]; $request = Yii::$app->request; $id = $request->get('id'); $model->congress_id = $id; $isModelLoaded = $model->load(Yii::$app->request->post()); if ($isModelLoaded) { //If data is recived from post then $modelsBoothSchedules = Model::createMultiple(BoothSchedules::classname()); Model::loadMultiple($modelsBoothSchedules, Yii::$app->request->post()); $model->date = $myDateTime = \DateTime::createFromFormat('m-d-Y', $model->date)->format('Y-m-d'); // validate all models $valid = $model->validate(); if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { //Skipping individual validation if ($flag = $model->save(false)) { //If data is saved in DB foreach ($modelsBoothSchedules as $modelBoothSchedule) { if (!empty($modelBoothSchedule->attendee_id)) { $attendeeDetails = $modelBoothSchedule->attendee; $modelBoothSchedule->booth_id = $model->id; $modelBoothSchedule->function_id = $attendeeDetails->function_Id; $modelBoothSchedule->cost_center_id = $attendeeDetails->group_Id; $modelBoothSchedule->isNewRecord = true; $modelBoothSchedule->isNewRecord = true; $flag = $modelBoothSchedule->save(false); if (!$flag) { //If data not saved then rollback the transaction and break the loop $transaction->rollBack(); break; } } } } if ($flag) { //If data is saved commit the transation and redirect to the view page $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } } catch (\Exception $e) { //In case of exeption rollback whatever done in db so far $transaction->rollBack(); } } } //Booth date should be between congress start date and end date $congressDetail = CongressDetails::findOne($model->congress_id); $congressStartDate = \DateTime::createFromFormat('Y-m-d', $congressDetail->start_date)->format('m-d-Y'); $congressEndDate = \DateTime::createFromFormat('Y-m-d', $congressDetail->end_date)->format('m-d-Y'); return $this->render('create', ['model' => $model, 'modelsBoothSchedules' => empty($modelsBoothSchedules) ? [new BoothSchedules()] : $modelsBoothSchedules, 'congressStartDate' => $congressStartDate, 'congressEndDate' => $congressEndDate]); }