addBehavior( new SoftDelete([ 'field' => 'deleted', 'value' => 1, ]) ); } public function beforeCreate() { $this->create_time = time(); } public function beforeUpdate() { if ($this->deleted == 1) { $this->published = 0; } $this->update_time = time(); } public function beforeSave() { if (empty($this->icon)) { $this->icon = kg_default_icon_path(); } elseif (Text::startsWith($this->icon, 'http')) { $this->icon = self::getIconPath($this->icon); } if (is_array($this->scopes) || is_object($this->scopes)) { $this->scopes = kg_json_encode($this->scopes); } } public function afterCreate() { $cache = new MaxTagIdCache(); $cache->rebuild(); } public function afterFetch() { if (!Text::startsWith($this->icon, 'http')) { $this->icon = kg_cos_icon_url($this->icon); } if (is_string($this->scopes) && $this->scopes != 'all') { $this->scopes = json_decode($this->scopes, true); } } public static function getIconPath($url) { if (Text::startsWith($url, 'http')) { return parse_url($url, PHP_URL_PATH); } return $url; } public static function scopeTypes() { return [ self::SCOPE_ARTICLE => '文章', self::SCOPE_QUESTION => '问答', self::SCOPE_COURSE => '课程', ]; } }