コード例 #1
0
 /**
  * Creates a new WebTag model.
  *
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new WebTag();
     $baseModel = null;
     if (Yii::$app->request->isPost) {
         $post = Yii::$app->request->post();
         $transaction = Yii::$app->db->beginTransaction();
         try {
             // Mise à jour du WebTag et du BaseTag associé
             if (!($baseModel = BaseTag::findOne($post['WebTag']['base_id']))) {
                 // Création du BaseTag à la volée & mise à jour de base_id dans $post
                 if ($post['BaseTag']['code']) {
                     $baseModel = new BaseTag();
                     if (!($baseModel->load($post) && $baseModel->save())) {
                         throw new Exception("Erreur à la création du BaseTag");
                     }
                     $post['WebTag']['base_id'] = $baseModel->id;
                 } else {
                     $msg = "Il faut choisir ou créer un BaseTag";
                     Yii::$app->session->addFlash('flash-danger', $msg);
                     throw new Exception($msg);
                 }
             }
             if (!($model->load($post) && $model->save())) {
                 throw new Exception("Erreur à la création du WebTag");
             }
             $transaction->commit();
             return $this->redirectAfterCreateSuccess($model);
         } catch (Exception $x) {
             $transaction->rollBack();
             Yii::$app->session->addFlash('flash-warning', HLib::t('messages', 'There are errors in your form'));
         }
     }
     if (!$baseModel) {
         $baseModel = new BaseTag();
     }
     // Affichage initial ou ré-affichage en cas d'erreur
     $baseTags = BaseTag::find()->orderBy('code')->all();
     $languages = Language::find()->orderBy('iso_639_code')->all();
     return $this->render('create', compact('model', 'baseTags', 'languages', 'baseModel'));
 }