addBehavior( new SoftDelete([ 'field' => 'deleted', 'value' => 1, ]) ); } public function beforeCreate() { $this->create_time = time(); } public function beforeUpdate() { if (time() - $this->update_time > 3 * 3600) { $sync = new QuestionIndexSync(); $sync->addItem($this->id); $sync = new QuestionScoreSync(); $sync->addItem($this->id); } $this->update_time = time(); } public function beforeSave() { if (Text::startsWith($this->cover, 'http')) { $this->cover = self::getCoverPath($this->cover); } if (is_array($this->tags) || is_object($this->tags)) { $this->tags = kg_json_encode($this->tags); } } public function afterCreate() { $cache = new MaxQuestionIdCache(); $cache->rebuild(); } public function afterFetch() { if (!empty($this->cover) && !Text::startsWith($this->cover, 'http')) { $this->cover = kg_cos_article_cover_url($this->cover); } if (is_string($this->tags)) { $this->tags = json_decode($this->tags, true); } } public static function getCoverPath($url) { if (Text::startsWith($url, 'http')) { return parse_url($url, PHP_URL_PATH); } return $url; } public static function publishTypes() { return [ self::PUBLISH_PENDING => '审核中', self::PUBLISH_APPROVED => '已发布', self::PUBLISH_REJECTED => '未通过', ]; } public static function sortTypes() { return [ 'latest' => '最新提问', 'active' => '最新回答', 'unanswered' => '尚未回答', ]; } }