Пример #1
0
 /**
  * Creates a new Person model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Person();
     $jobModel = new Job();
     if ($jobModel->load(Yii::$app->request->post()) && $jobModel->validate() && $model->load(Yii::$app->request->post()) && $model->validate()) {
         $transaction = $model->getDb()->beginTransaction();
         try {
             if (is_numeric($jobModel->name)) {
                 $jobModel = $jobModel->findOne($jobModel->name);
             } else {
                 $jobModel = new Job();
                 $jobModel->load(Yii::$app->request->post());
                 if (!$jobModel->save(false)) {
                     throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($jobModel->tableName())), 'msj' => print_r($jobModel->getErrors(), true)]), 500);
                 }
             }
             $model->job_id = $jobModel->id;
             if (!$model->save(false)) {
                 throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($model->tableName())), 'msj' => print_r($model->getErrors(), true)]), 500);
             }
             $transaction->commit();
         } catch (\Exception $e) {
             $transaction->rollBack();
             throw $e;
         }
         return $this->redirect(['index']);
     } else {
         return $this->render('create', ['model' => $model, 'jobModel' => $jobModel]);
     }
 }