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])) {