/** * Return list of relations for current model * @return array */ public static function getRelations() { $old = parent::getRelations(); unset($old['replies']); $old['parent'] = [DbRelations::BELONGS_TO, '\\mpf\\modules\\forum\\models\\ForumReplySeventh', 'reply_id']; $old['replies'] = DbRelation::hasMany(ForumReplyNth::className())->columnsEqual('id', 'reply_id')->hasValue('level', 9); $old['myVote'] = DbRelation::hasOne(ForumReplyVote::className())->columnsEqual('id', 'reply_id')->hasValue('level', 8)->hasValue('user_id', WebApp::get()->user()->isConnected() ? WebApp::get()->user()->id : 0); return $old; }
/** * It will save thread info(update with last reply info) + subcategory info * @param int $sectionId * @param int $level * @param int $userId * @param int $userGroupID * @param int $time * @return bool */ public function saveReply($sectionId, $level = 1, $userId = null, $userGroupID = null, $time = null) { $this->user_id = is_null($userId) ? WebApp::get()->user()->id : $userId; $this->time = date('Y-m-d H:i:s', $time ?: time()); $this->section_id = $sectionId; $this->score = $this->edited = $this->edit_user_id = $this->deleted = $this->deleted_user_id = 0; $this->user_group_id = $userGroupID ?: UserAccess::get()->getUserGroup($sectionId, true); if (!$this->save()) { return false; } $thread = ForumThread::findByPk($this->thread_id); $thread->replies = ($firstLevelReplies = ForumReply::countByAttributes(['thread_id' => $this->thread_id])) + ForumReplySecond::countByAttributes(['thread_id' => $this->thread_id]) + ForumReplyThird::countByAttributes(['thread_id' => $this->thread_id]) + ForumReplyForth::countByAttributes(['thread_id' => $this->thread_id]) + ForumReplyFifth::countByAttributes(['thread_id' => $this->thread_id]) + ForumReplySixth::countByAttributes(['thread_id' => $this->thread_id]) + ForumReplySeventh::countByAttributes(['thread_id' => $this->thread_id]) + ForumReplyEighth::countByAttributes(['thread_id' => $this->thread_id]) + ForumReplyNth::countByAttributes(['thread_id' => $this->thread_id]); $thread->first_level_replies = $firstLevelReplies; $thread->last_reply_id = $this->id; $thread->last_reply_user_id = $this->user_id; $thread->last_reply_level = $level; $thread->last_reply_date = $this->time; $thread->save(false); $thread->subcategory->last_active_thread_id = $thread->id; $thread->subcategory->last_activity_time = $this->time; $thread->subcategory->last_activity = 'reply'; $thread->subcategory->last_active_user_id = $this->user_id; $thread->subcategory->recalculateNumbers()->save(); return true; }