/** * Updates an existing Article model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $dataModel = ArticleData::findOne($id); if ($model->load(Yii::$app->request->post()) && $dataModel->load(Yii::$app->request->post())) { $isValid = $model->validate(); $isValid = $dataModel->validate() && $isValid; if ($isValid) { $model->save(false); $dataModel->save(false); return $this->redirect(['index']); } } return $this->render('update', ['model' => $model, 'dataModel' => $dataModel]); }
public function actionUpdateArticle($id) { $userId = \Yii::$app->user->id; $model = Article::find()->where(['id' => $id, 'user_id' => $userId])->one(); $dataModel = ArticleData::find()->where(['id' => $id])->one(); if (!isset($model, $dataModel)) { throw new NotFoundHttpException('文章不存在!'); } if ($model->load(\Yii::$app->request->post()) && $dataModel->load(\Yii::$app->request->post())) { $isValid = $model->validate(); $isValid = $dataModel->validate() && $isValid; if ($isValid) { $model->save(false); $dataModel->save(false); \Yii::$app->session->setFlash('success', '修改成功,请等待管理员审核!'); return $this->redirect(['update-article', 'id' => $id]); } } return $this->render('update-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; }
public function getData() { return $this->hasOne(ArticleData::className(), ['id' => 'id']); }