Skip to content

Commit

Permalink
改进关联模型的输出
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Aug 19, 2023
1 parent 38a6da3 commit 5d13905
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/model/concern/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function toArray(): array
$val->hidden($hidden[$key], true);
}
// 关联模型对象
if (!isset($hidden[$key]) || true !== $hidden[$key]) {
if (array_key_exists($key,$this->with) && (!isset($hidden[$key]) || true !== $hidden[$key])) {
$item[$key] = $val->toArray();
}
} elseif (isset($visible[$key])) {
Expand Down
9 changes: 9 additions & 0 deletions src/model/concern/RelationShip.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ trait RelationShip
*/
private $relation = [];

/**
* 预载入关联模型
*
* @var array
*/
protected $with = [];

/**
* 关联写入定义信息.
*
Expand Down Expand Up @@ -347,6 +354,8 @@ public function eagerlyResult(array $relations, array $withRelationAttr = [], bo
}

$relationResult->eagerlyResult($this, $relationName, $subRelation, $closure, $relationCache, $join);

$this->with[$relationName] = true;
}
}

Expand Down

0 comments on commit 5d13905

Please sign in to comment.