public function actionUserInfo() { UserInfo::updateAll(['thanks_count' => 0, 'like_count' => 0, 'hate_count' => 0]); $meta = UserMeta::find()->all(); foreach ($meta as $key => $value) { if (in_array($value->type, ['thanks', 'like', 'hate'])) { switch ($value->target_type) { case 'topic': case 'post': $this->stdout("同步文章操作……\n"); $topic = Topic::findOne($value->target_id); if (UserInfo::updateAllCounters([$value->type . '_count' => 1], ['user_id' => $topic->user_id])) { $this->stdout("同步评论成功`(*∩_∩*)′\n"); } else { $this->stdout("同步评论失败::>_<::\n"); } break; case 'comment': $this->stdout("同步评论操作……\n"); $comment = PostComment::findOne($value->target_id); if (UserInfo::updateAllCounters([$value->type . '_count' => 1], ['user_id' => $comment->user_id])) { $this->stdout("同步评论成功`(*∩_∩*)′\n"); } else { $this->stdout("同步评论失败::>_<:: \n"); } break; default: # code... break; } } } return; }
/** * 获取关注者 * @return static */ public function getFollower() { return $this->hasMany(UserMeta::className(), ['target_id' => 'id'])->where(['target_type' => self::TYPE, 'type' => 'follow']); }
public function actionSync() { UserInfo::updateAll(['thanks_count' => 0, 'like_count' => 0, 'hate_count' => 0]); $meta = UserMeta::find()->all(); foreach ($meta as $key => $value) { if (in_array($value->type, ['thanks', 'like', 'hate'])) { switch ($value->target_type) { case 'topic': case 'post': echo '同步文章操作</br>'; $topic = Topic::findOne($value->target_id); UserInfo::updateAllCounters([$value->type . '_count' => 1], ['user_id' => $topic->user_id]); break; case 'comment': echo '同步评论操作</br>'; $comment = PostComment::findOne($value->target_id); UserInfo::updateAllCounters([$value->type . '_count' => 1], ['user_id' => $comment->user_id]); break; default: # code... break; } } } return; }
public function getLike() { $model = new UserMeta(); return $model->isUserAction(self::TYPE, 'like', $this->id); }
/** * 最新收藏 * @param string $username * @return string * @throws NotFoundHttpException */ public function actionFavorite($username = '') { $user = $this->user($username); $dataProvider = new ActiveDataProvider(['query' => UserMeta::find()->where(['user_id' => $user->id, 'type' => 'favorite', 'target_type' => 'topic'])->orderBy(['created_at' => SORT_DESC])]); return $this->render('show', ['user' => $user, 'dataProvider' => $dataProvider]); }
/** * 喝倒彩或者赞 * @param User $user * @param Post $model * @param $action 动作 * @return array */ protected static function toggleType(User $user, Post $model, $action) { $data = ['target_id' => $model->id, 'target_type' => $model::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) { // 如果是新增数据, 删除掉Hate的同类型数据 $attributeName = $action == 'like' ? 'hate' : 'like'; $attributes = [$action . '_count' => 1]; if (UserMeta::deleteOne($data + ['type' => $attributeName])) { // 如果有删除hate数据, hate_count也要-1 $attributes[$attributeName . '_count'] = -1; } //更新版块统计 $model->updateCounters($attributes); // 更新个人总统计 UserInfo::updateAllCounters($attributes, ['user_id' => $model->user_id]); } return [$result, $userMeta]; } $model->updateCounters([$action . '_count' => -1]); UserInfo::updateAllCounters([$action . '_count' => -1], ['user_id' => $model->user_id]); return [true, null]; }
/** * @param $userId * @param $type * @param string $targetType * @return ActiveDataProvider */ protected function userMeta($userId, $type, $targetType = 'topic') { return new ActiveDataProvider(['query' => UserMeta::find()->where(['user_id' => $userId, 'type' => $type, 'target_type' => $targetType])->orderBy(['created_at' => SORT_DESC])]); }