fix(api): 修复getData(id)新建对象时抛异常,改用isset安全检查

This commit is contained in:
augushong
2026-05-01 16:19:38 +08:00
parent 2f5846225e
commit eab8cee8a8

View File

@@ -270,11 +270,12 @@ class Post extends Base
public function getHitsTitleAttr() public function getHitsTitleAttr()
{ {
// 新建对象时直接返回 0 // 新建对象时直接返回 0
$id = $this->getData('id'); if (!isset($this->data['id']) || empty($this->data['id'])) {
if (empty($id)) {
return 0; return 0;
} }
$id = $this->data['id'];
$cache_key = static::CACHE_KEY_HITS . $id; $cache_key = static::CACHE_KEY_HITS . $id;
$value = Cache::get($cache_key); $value = Cache::get($cache_key);
@@ -294,11 +295,12 @@ class Post extends Base
public function setHitsAttr($value) public function setHitsAttr($value)
{ {
// 新建对象(无 id直接返回值跳过缓存操作 // 新建对象(无 id直接返回值跳过缓存操作
$id = $this->getData('id'); if (!isset($this->data['id']) || empty($this->data['id'])) {
if (empty($id)) {
return $value; return $value;
} }
$id = $this->data['id'];
$cache_key = static::CACHE_KEY_HITS . $id; $cache_key = static::CACHE_KEY_HITS . $id;
Cache::inc($cache_key); Cache::inc($cache_key);