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 9f7357b commit 499739d
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/model/concern/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
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 @@ -409,6 +411,10 @@ 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 @@ -421,7 +427,7 @@ public function setAttr(string $name, $value, array $data = []): void
$value = $this->writeTransform($value, $this->type[$name]);
} elseif ($this->isRelationAttr($name)) {
// 关联属性
$this->relation[$name] = $value;
$this->relation[$name] = $value;
} elseif ((array_key_exists($name, $this->origin) || empty($this->origin)) && $value instanceof Stringable) {
// 对象类型
$value = $value->__toString();
Expand Down

0 comments on commit 499739d

Please sign in to comment.