/** * 添加评论 * @param Comment $comment * @param bool $active 激活 * @return bool */ public function addComment(Comment $comment, $active = false) { $comment->setAttributes(['tid' => $this->id, 'fid' => $this->fid]); $result = $comment->save(); if ($result) { $active && $comment->setActive(); return true; } return false; }
public function commentInit() { if (!$this->topic) { return; } Console::output('创建默认评论 ....'); $comment = new Comment(); $comment->setAttributes(['author_id' => 1, 'content' => '默认评论内容']); $message = $this->topic->addComment($comment, true) ? '成功' : '失败'; Console::output('创建默认评论' . $message); }
public function commentInit() { if (!$this->topic) { return; } echo PHP_EOL . '创建默认评论 ....' . PHP_EOL; $comment = new Comment(); $comment->setAttributes(['author_id' => 1, 'content' => '默认评论内容']); $message = $this->topic->addComment($comment) ? '成功' : '失败'; echo PHP_EOL . '创建默认评论' . $message . PHP_EOL; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params, CommentQuery $query = null) { $query === null && ($query = Comment::find()); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'fid' => $this->fid, 'tid' => $this->tid, 'author_id' => $this->author_id, 'view_count' => $this->view_count, 'comment_count' => $this->comment_count, 'like_count' => $this->like_count, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'subject', $this->subject])->andFilterWhere(['like', 'content', $this->content]); return $dataProvider; }
/** * 创建新评论 * @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; }
/** * 获取评论列表 * @return ActiveQuery */ public function getComments() { return $this->hasMany(Comment::tableName(), ['id' => 'tid']); }