findTopic() public static method

通过ID获取指定话题
public static findTopic ( $id ) : array | Topic | null | ActiveRecord
$id
return array | Topic | null | ActiveRecord
示例#1
0
 /**
  * 创建评论
  * @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;
 }
示例#2
0
 public function userDoAction($id, $action)
 {
     $topic = Topic::findTopic($id);
     $user = \Yii::$app->user->getIdentity();
     if (in_array($action, ['like', 'hate'])) {
         return UserService::TopicActionA($user, $topic, $action);
     } else {
         return UserService::TopicActionB($user, $topic, $action);
     }
 }
 /**
  * 伪删除
  * @param $id
  * @return \yii\web\Response
  * @throws NotFoundHttpException
  */
 public function actionExcellent($id)
 {
     $user = Yii::$app->user->identity;
     $model = Topic::findTopic($id);
     if ($user && ($user->isAdmin($user->username) || $user->isSuperAdmin($user->username))) {
         $action = $model->status == Topic::STATUS_ACTIVE ? Topic::STATUS_GOOD : Topic::STATUS_ACTIVE;
         $model->updateAttributes(['status' => $action]);
         $this->flash("操作成功", 'success');
         return $this->redirect(['/topic/default/view', 'id' => $model->id]);
     } else {
         throw new NotFoundHttpException();
     }
 }
示例#4
0
 /**
  * 加精华
  * @param $id
  * @return \yii\web\Response
  * @throws NotFoundHttpException
  */
 public function actionExcellent($id)
 {
     $user = Yii::$app->user->identity;
     $model = Topic::findTopic($id);
     if ($user && ($user->isAdmin($user->username) || $user->isSuperAdmin($user->username))) {
         TopicService::excellent($model);
         $this->flash("操作成功", 'success');
         return $this->redirect(['/topic/default/view', 'id' => $model->id]);
     } else {
         throw new NotFoundHttpException();
     }
 }