/** * @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); }
public function afterSave($insert, $changedAttributes) { 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(); if ($topic->user_id != $this->user_id) { $notice = new Notice(); $notice->from_user_id = $this->user_id; $notice->to_user_id = $topic->user_id; $notice->topic_id = $this->follow_id; $notice->type = 3; $notice->msg = ''; $notice->created = time(); $notice->save(); } } return parent::afterSave($insert, $changedAttributes); }