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