/** * 发表新帖 * @param Topic $topic * @param bool $active 激活 * @return bool */ public function addTopic(Topic $topic, $active = false) { $topic->setAttributes(['fid' => $this->id]); $result = $topic->save(); if ($result) { $active && $topic->setActive(); return true; } return false; }
public function topicInit() { if ($this->forumId === null) { echo PHP_EOL . '无法创建默认话题,因为没有默认归属版块 ....' . PHP_EOL; return; } echo PHP_EOL . '创建默认话题 ....' . PHP_EOL; $topic = new Topic(); $topic->setAttributes(['fid' => $this->forumId, 'author_id' => 1, 'subject' => '默认话题', 'content' => '默认话题内容']); if ($topic->save()) { $message = '成功'; $this->topic = $topic; } else { $message = '失败'; } echo PHP_EOL . '创建默认话题' . $message . PHP_EOL; }
public function topicInit() { if ($this->forumId === null) { Console::output('无法创建默认话题,因为没有默认归属版块 ....'); return; } Console::output('创建默认话题 ....'); $topic = new Topic(); $topic->setAttributes(['fid' => $this->forumId, 'author_id' => 1, 'subject' => '默认话题', 'content' => '默认话题内容']); if ($topic->save()) { $topic->setActive(); //激活 $message = '成功'; $this->topic = $topic; } else { $message = '失败'; } Console::output('创建默认话题' . $message); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Topic::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC], 'attributes' => ['created_at']]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'fid' => $this->fid, 'comment_count' => $this->comment_count, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'subject', $this->subject])->andFilterWhere(['like', 'content', $this->content]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params, $active = true) { $query = Topic::find(); $active && $query->active(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'fid' => $this->fid, 'comment_count' => $this->comment_count, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'subject', $this->subject])->andFilterWhere(['like', 'content', $this->content])->orderBy(['is_topic' => SORT_DESC]); return $dataProvider; }
/** * 获取话题 */ public function getTopic() { return $this->hasOne(Topic::className(), ['id' => 'tid']); }
/** * 创建新评论 * @param $topic * @return Comment */ protected function newTopic(Forum $forum) { $model = new Topic(); $request = Yii::$app->request; if ($model->load($request->post())) { $model->author_id = Yii::$app->user->id; if ($forum->addTopic($model, true)) { if ($tags = $request->post('tags')) { $tags = Tag::find()->where(['name' => explode(',', $tags)])->active()->all(); $model->addTags($tags); } $this->flash('发表话题成功!', 'success'); return Yii::$app->end(0, $this->redirect(['topic/view', 'id' => $model->id])); } } return $model; }
/** * 创建新评论 * @param $topic * @return Comment */ protected function newComment(Topic $topic) { $model = new Comment(); if ($model->load(Yii::$app->request->post())) { $model->author_id = Yii::$app->user->id; if ($topic->addComment($model, true)) { $this->flash('发表评论成功!', 'success'); Yii::$app->end(0, $this->refresh()); } } return $model; }