Skip to content

Commit

Permalink
改进模型对象输出
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Sep 1, 2023
1 parent 499739d commit e676e17
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 7 deletions.
7 changes: 1 addition & 6 deletions src/model/concern/Attribute.php
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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));
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/model/concern/Conversion.php
Expand Up @@ -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])) {
Expand Down

0 comments on commit e676e17

Please sign in to comment.