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')); }
/** * 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 addPost() { if ($this->validate()) { $content = new Content(); $content->name = $this->name; $content->slug = $this->slug; $content->text_bb = $this->text_bb; $content->author_id = Yii::$app->user->id; $content->category_id = 1; $content->text_short = $this->text_short; $content->status = $this->status == 1 ? 1 : 0; if ($content->save()) { return $content; } } return null; }
/** * 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]); }