From e676e172d54055266005986d05685bdac5ac66b7 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 1 Sep 2023 17:08:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=A8=A1=E5=9E=8B=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/model/concern/Attribute.php | 7 +------ src/model/concern/Conversion.php | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/model/concern/Attribute.php b/src/model/concern/Attribute.php index a6ca470e..6265ae80 100644 --- a/src/model/concern/Attribute.php +++ b/src/model/concern/Attribute.php @@ -18,8 +18,6 @@ use Stringable; use think\db\Raw; use think\helper\Str; -use think\Model; -use think\model\Collection as ModelCollection; use think\model\Relation; /** @@ -411,10 +409,6 @@ public function setAttr(string $name, $value, array $data = []): void // 检测修改器 $method = 'set' . Str::studly($name) . 'Attr'; - if ($value instanceof Model || $value instanceof ModelCollection) { - $this->with[$name] = true; - } - if (method_exists($this, $method)) { $array = $this->data; $value = $this->$method($value, array_merge($this->data, $data)); @@ -428,6 +422,7 @@ public function setAttr(string $name, $value, array $data = []): void } elseif ($this->isRelationAttr($name)) { // 关联属性 $this->relation[$name] = $value; + $this->with[$name] = true; } elseif ((array_key_exists($name, $this->origin) || empty($this->origin)) && $value instanceof Stringable) { // 对象类型 $value = $value->__toString(); diff --git a/src/model/concern/Conversion.php b/src/model/concern/Conversion.php index a26d8e52..c972769e 100644 --- a/src/model/concern/Conversion.php +++ b/src/model/concern/Conversion.php @@ -264,7 +264,7 @@ public function toArray(): array $val->hidden($hidden[$key], true); } // 关联模型对象 - if (array_key_exists($key, $this->with) && (!isset($hidden[$key]) || true !== $hidden[$key])) { + if (!array_key_exists($key, $this->relation) || (array_key_exists($key, $this->with) && (!isset($hidden[$key]) || true !== $hidden[$key]))) { $item[$key] = $val->toArray(); } } elseif (isset($visible[$key])) {