/** * 创建评论 * @param $id * @return PostComment|\yii\web\Response */ public function actionCreate($id) { $post = Topic::findTopic($id); $model = new PostComment(); if ($model->load(Yii::$app->request->post())) { $topService = new TopicService(); if (!$topService->filterContent($model->comment)) { $this->flash('回复内容请勿回复无意义的内容,如你想收藏或赞等功能,请直接操作这篇帖子。', 'warning'); return $this->redirect(['/topic/default/view', 'id' => $id]); } $model->user_id = Yii::$app->user->id; $model->post_id = $id; $model->ip = Yii::$app->getRequest()->getUserIP(); $rawComment = $model->comment; $model->comment = $model->replace($rawComment); if ($model->save()) { (new UserMeta())->saveNewMeta('topic', $id, 'follow'); (new NotificationService())->newReplyNotify(Yii::$app->user->identity, $post, $model, $rawComment); // 更新回复时间 $post->lastCommentToUpdate(Yii::$app->user->identity->username); // 评论计数器 Topic::updateAllCounters(['comment_count' => 1], ['id' => $post->id]); // 更新个人总统计 UserInfo::updateAllCounters(['comment_count' => 1], ['user_id' => $model->user_id]); $this->flash("评论成功", 'success'); } else { $this->flash(array_values($model->getFirstErrors())[0], 'warning'); } return $this->redirect(['/topic/default/view', 'id' => $post->id]); } return $model; }
/** * 创建评论 * @param Post $post * @return array|PostComment|string */ protected function newComment(Post $post) { $model = new PostComment(); if ($model->load(Yii::$app->request->post())) { $topService = new TopicService(); if (!$topService->filterContent($model->comment)) { $this->flash('回复内容请勿回复无意义的内容,如你想收藏或赞等功能,请直接操作这篇帖子。', 'warning'); return $this->redirect(['view', 'id' => $post->id]); } $model->user_id = Yii::$app->user->id; $model->post_id = $post->id; $model->ip = Yii::$app->getRequest()->getUserIP(); $rawComment = $model->comment; $model->comment = $model->replace($rawComment); if ($model->save()) { (new UserMeta())->saveNewMeta('topic', $post->id, 'follow'); (new NotificationService())->newReplyNotify(Yii::$app->user->identity, $post, $model, $rawComment); // 评论计数器 Topic::updateAllCounters(['comment_count' => 1], ['id' => $post->id]); // 更新个人总统计 UserInfo::updateAllCounters(['comment_count' => 1], ['user_id' => $model->user_id]); $this->flash("评论成功", 'success'); return $this->redirect(['view', 'id' => $post->id]); } } return $model; }