public function actionUndo($type, $follow) { $model = Follow::findOne(['user_id' => Yii::$app->user->id, 'type' => $type, 'follow_id' => $follow]); if ($model !== null) { if ($model->delete() && $type == 3) { $topic = Topic::findOne($follow); $topic->follow = $topic->follow - 1; $topic->save(); } } $next = Yii::$app->request->get('next'); if (isset($next)) { return $this->redirect($next); } else { $this->goHome(); } }
/** * @param boolean $insert * @param array $changedAttributes * @return bool */ public function afterSave($insert, $changedAttributes) { $topic = Topic::findOne($this->topic_id); $topic->reply = $topic->reply + 1; $topic->last_reply_time = time(); $topic->updated_at = time(); $topic->last_reply_user = Yii::$app->user->identity->username; $rst = $topic->update(); Yii::$app->cache->delete('HotTopic8'); //给用户发回复或者@通知,回复自己的不通知 if ($rst && Yii::$app->user->id != $topic->user_id) { $notice = new Notice(); $notice->from_user_id = $this->user_id; $notice->to_user_id = $topic->user_id; $notice->topic_id = $this->topic_id; $notice->type = 1; $notice->msg = $this->content; $notice->created = time(); $notice->save(); } //回复中提到其他人,通知其他人 if (strstr($this->content, '@')) { preg_match_all('/@(.*?)\\s/', $this->content, $match); if (isset($match[1]) && count($match[1]) > 0) { $notice_user = array_unique($match[1]); foreach ($notice_user as $v) { $to_user = User::findOne(['username' => $v]); if ($v == $topic->user->username || $v == Yii::$app->user->identity->username || empty($to_user->id)) { continue; } $notice = new Notice(); $notice->from_user_id = $this->user_id; $notice->to_user_id = $to_user->id; $notice->topic_id = $this->topic_id; $notice->type = 2; $notice->msg = $this->content; $notice->created = time(); $notice->save(); } } } return parent::afterSave($insert, $changedAttributes); }
/** * Finds the Topic model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Topic the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Topic::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function afterDelete() { if ($this->type == 1) { Yii::$app->cache->delete('UserCount' . $this->user_id); Yii::$app->cache->delete('FollowUserId' . Yii::$app->user->id); Yii::$app->cache->delete('FollowUser' . Yii::$app->user->id); } if ($this->type == 2) { Yii::$app->cache->delete('NodeCount' . $this->user_id); Yii::$app->cache->delete('FollowNodeId' . Yii::$app->user->id); Yii::$app->cache->delete('FollowNode' . Yii::$app->user->id); } if ($this->type == 3) { Yii::$app->cache->delete('TopicCount' . $this->user_id); Yii::$app->cache->delete('NoticeCount' . $this->follow_id); Yii::$app->cache->delete('FollowTopic' . Yii::$app->user->id); $topic = Topic::findOne($this->follow_id); $topic->follow = $topic->follow - 1; $topic->save(); } return parent::afterDelete(); }