Beispiel #1
0
 /**
  * 对评论点赞
  * @param User $user
  * @param PostComment $comment
  * @param $action
  * @return array
  */
 public static function CommentAction(User $user, PostComment $comment, $action)
 {
     $data = ['target_id' => $comment->id, 'target_type' => $comment::TYPE, 'user_id' => $user->id, 'value' => '1'];
     if (!UserMeta::deleteOne($data + ['type' => $action])) {
         // 删除数据有行数则代表有数据,无行数则添加数据
         $userMeta = new UserMeta();
         $userMeta->setAttributes($data + ['type' => $action]);
         $result = $userMeta->save();
         if ($result) {
             $comment->updateCounters([$action . '_count' => 1]);
             // 更新个人总统计
             UserInfo::updateAllCounters([$action . '_count' => 1], ['user_id' => $comment->user_id]);
         }
         return [$result, $userMeta];
     }
     $comment->updateCounters([$action . '_count' => -1]);
     // 更新个人总统计
     UserInfo::updateAllCounters([$action . '_count' => -1], ['user_id' => $comment->user_id]);
     return [true, null];
 }