From 45efbc24f693c4fd32aefd4a83065e7e0419c5e5 Mon Sep 17 00:00:00 2001 From: augushong Date: Sat, 2 May 2026 21:24:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(Post):=20=E4=BF=AE=E6=AD=A3=E7=82=B9?= =?UTF-8?q?=E5=87=BB=E6=95=B0=E7=BC=93=E5=AD=98=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用 getOrigin 方法替代直接访问 data 数组,提高健壮性 - 修复缓存值为空字符串时的类型转换问题 - 在 setHitsAttr 中预加载 hits_title 属性确保缓存存在 - 统一使用整型返回值,避免类型不一致 --- app/model/Post.php | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/app/model/Post.php b/app/model/Post.php index 0380a23..fba868d 100644 --- a/app/model/Post.php +++ b/app/model/Post.php @@ -269,24 +269,20 @@ class Post extends Base public function getHitsTitleAttr() { - // 新建对象时直接返回 0 - if (!isset($this->data['id']) || empty($this->data['id'])) { + $id = $this->getOrigin('id'); + if (empty($id)) { return 0; } - $id = $this->data['id']; - $cache_key = static::CACHE_KEY_HITS . $id; $value = Cache::get($cache_key); - if (!is_null($value)) { - return $value; + if (!is_null($value) && $value !== '') { + return (int) $value; } - // 安全获取:hits 可能尚未存在于 data 中 - $value = array_key_exists('hits', $this->data) ? $this->data['hits'] : 0; - + $value = (int) ($this->getOrigin('hits') ?: 0); Cache::set($cache_key, $value, 600); return $value; @@ -294,15 +290,14 @@ class Post extends Base public function setHitsAttr($value) { - // 新建对象(无 id)时,直接返回值,跳过缓存操作 - if (!isset($this->data['id']) || empty($this->data['id'])) { + $id = $this->getOrigin('id'); + if (empty($id)) { return $value; } - $id = $this->data['id']; - $cache_key = static::CACHE_KEY_HITS . $id; + $this->getAttr('hits_title'); Cache::inc($cache_key); return $value;