/** * Creates a new Article model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Article(); $dataModel = new ArticleData(); if ($model->load(Yii::$app->request->post()) && $dataModel->load(Yii::$app->request->post())) { $isValid = $model->validate(); if ($isValid) { $model->save(false); $dataModel->id = $model->id; $isValid = $dataModel->validate(); if ($isValid) { $dataModel->save(false); return $this->redirect(['index']); } } } return $this->render('create', ['model' => $model, 'dataModel' => $dataModel]); }
public function actionCreateArticle() { $model = new Article(); $dataModel = new ArticleData(); if ($model->load(\Yii::$app->request->post()) && $dataModel->load(\Yii::$app->request->post())) { $isValid = $model->validate(); if ($isValid) { $model->save(false); $dataModel->id = $model->id; $isValid = $dataModel->validate(); if ($isValid) { $dataModel->save(false); \Yii::$app->session->setFlash('success', '投稿成功,请等待管理员审核!'); return $this->redirect(['create-article']); } } } return $this->render('create-article', ['model' => $model, 'dataModel' => $dataModel]); }
/** * 将文章插入数据库 * @param $title 标题 * @param $content 内容 * @param $publish_at 发布时间 * @param string $category 分类名 * @param string $cover 封面 * @return int */ public function insert($title, $content, $publish_at, $category = '', $cover = '') { //插入标签(搜索的分类) $categoryId = (new Category())->getCategoryIdByName($category); if (!$categoryId) { throw new Exception('该分类不存在'); } $article = new Article(); $article->title = $title; $article->author = '匿名'; $article->status = 1; $article->category = $category; $article->category_id = $categoryId; $article->source = $this->config['domain']; $article->cover = $cover; $article->created_at = $publish_at; $article->user_id = 0; $res = $article->save(false); if ($res) { $articleData = new ArticleData(); $articleData->id = $article->id; $articleData->content = $content; $res = $articleData->save(false); } return $res ? 1 : 0; }