public function actionCreate()
 {
     $content = new Content();
     if ($content->load($_POST) && $content->save()) {
         Yii::$app->session->setFlash('success', Yii::t('app', "New Content {$content->name} created successfully"));
         return $this->redirect(['update', 'id' => $content->id]);
     }
     return $this->render('create', compact('content'));
 }
Beispiel #2
0
 /**
  * Creates a new Content model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Content();
     $model->setScenario(Content::SCENARIO_PAGE);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 public function actionCreate()
 {
     $content = new Content();
     $translations = $content->initializeTranslations();
     if ($content->load($_POST) && Model::loadMultiple($translations, $_POST) && Model::validateMultiple($translations) && $content->save()) {
         $content->saveTranslations($translations);
         Yii::$app->session->setFlash('success', Yii::t('app', "Content {$content->name} created successfully"));
         return $this->redirect(['index']);
     }
     return $this->render('create', compact('content', 'translations'));
 }
 public function actionCreate()
 {
     $model = new Content();
     if ($model->load(Yii::$app->request->post())) {
         $model->avatarFile = UploadedFile::getInstance($model, 'avatarFile');
         if ($model->save() && $model->upload()) {
             Yii::$app->session->setFlash("success", "Create Content Successful");
             return $this->redirect(['update', 'id' => $model->id]);
         }
     }
     return $this->render("create", ['model' => $model]);
 }
Beispiel #5
0
 /**
  * Creates a new Content model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Content(['scenario' => Content::SCENARIO_ARTICLE]);
     if ($model->load(Yii::$app->request->post())) {
         //事物匿名函数
         $callback = function () use($model) {
             $post = Yii::$app->request->post($model->formName());
             if ($model->save() && $model->saveTags($post['tags']) && $model->saveCategory($post['category'])) {
                 return true;
             }
             throw new ErrorException('更新数据失败');
         };
         try {
             //用数据库事物来保存数据,以免只更新了其中一个
             if ($model->getDb()->transaction($callback)) {
                 //如果两个保存成功,则跳转到视图页面预览新增的文章
                 return $this->redirect(['view', 'id' => $model->id]);
             }
         } catch (Exception $e) {
             Yii::$app->session->setFlash('error', $e->getMessage());
         }
     }
     return $this->render('create', ['model' => $model]);
 }