public function actionCreate($node = null) { if (Yii::$app->user->isGuest) { Yii::$app->getSession()->setFlash('danger', '你需要登陆之后才能创作新主题'); return $this->redirect('/account/login?next=/topic/create'); } if (Yii::$app->user->identity->email_status == 0) { $this->goHome(); } $this->title = '创作新主题 - ' . Yii::$app->name; $this->description = ''; if (!empty($node)) { if (($model = Node::findOne(['enname' => $node])) !== null) { $node = $model; } else { $node = null; } } $model = new Topic(); if (!empty($node)) { $model->node_id = $node->id; } $topicContent = new TopicContent(); if ($model->load(Yii::$app->request->post()) && $model->create()) { $this->redirect('/topic/' . $model->id); } else { return $this->render('create', ['model' => $model, 'node' => $node, 'topicContent' => $topicContent]); } }
public function create() { $this->created = time(); $this->user_id = Yii::$app->user->id; $this->need_login = Node::findOne($this->node_id)->need_login; $this->updated_at = time(); if ($this->validate()) { if ($this->save()) { Yii::$app->cache->delete('TopicCount'); Yii::$app->cache->delete('Ranking'); Yii::$app->cache->delete('HotNode15'); $topicContent = new TopicContent(); if ($topicContent->load(Yii::$app->request->post())) { $search = new Search(); $search->topic_id = $this->id; $search->title = $this->title; $search->content = $topicContent->content; $search->save(); $topicContent->topic_id = $this->id; $topicContent->created = time(); return $topicContent->save(); } } } return false; }
/** * Finds the Topic model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Topic the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($name) { if (($model = Node::findOne(['enname' => $name])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }