コード例 #1
0
 /**
  * Creates a new Sda model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Sda();
     $modelsSdaSchedules = [new SdaSchedules()];
     $request = Yii::$app->request;
     $id = $request->get('id');
     $model->congress_id = $id;
     $isModelLoaded = $model->load(Yii::$app->request->post());
     if ($isModelLoaded) {
         $modelsSdaSchedules = Model::createMultiple(SdaSchedules::classname());
         Model::loadMultiple($modelsSdaSchedules, Yii::$app->request->post());
         $model->date = $myDateTime = \DateTime::createFromFormat('m-d-Y', $model->date)->format('Y-m-d');
         // validate all models
         $valid = $model->validate();
         //$valid = && $valid;
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     foreach ($modelsSdaSchedules as $modelSdaSchedule) {
                         if (!empty($modelSdaSchedule->attendee_id)) {
                             $modelSdaSchedule->sda_id = $model->id;
                             $modelSdaSchedule->congress_id = $model->congress_id;
                             $modelSdaSchedule->assigned = 0;
                             $modelSdaSchedule->isNewRecord = true;
                             $flag = $modelSdaSchedule->save(false);
                             if (!$flag) {
                                 $transaction->rollBack();
                                 break;
                             }
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     }
     //SDA 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, 'modelsSdaSchedules' => empty($modelsSdaSchedules) ? [new SdaSchedules()] : $modelsSdaSchedules, 'congressStartDate' => $congressStartDate, 'congressEndDate' => $congressEndDate]);
 }