Skip to content

Commit

Permalink
修正关联数据集读取
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Feb 27, 2025
1 parent 1097b61 commit b1dd14d
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 40 deletions.
7 changes: 4 additions & 3 deletions src/db/concern/ModelRelationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public function withJoin(array | string $with, string $joinType = '')
*
* @return $this
*/
protected function withAggregate(string | array $relations, string $aggregate = 'count', $field = '*', bool $subQuery = true)
protected function withAggregate(string | array $relations, string $aggregate = 'count', $field = 'id', bool $subQuery = true)
{
if (empty($this->model)) {
return $this;
Expand Down Expand Up @@ -476,13 +476,14 @@ public function withCache(string | array | bool $relation = true, $key = true, $
* 关联统计
*
* @param string|array $relation 关联方法名
* @param string $field 字段(默认为id)
* @param bool $subQuery 是否使用子查询
*
* @return $this
*/
public function withCount(string | array $relation, bool $subQuery = true)
public function withCount(string | array $relation, string $field = 'id', bool $subQuery = true)
{
return $this->withAggregate($relation, 'count', '*', $subQuery);
return $this->withAggregate($relation, 'count', $field, $subQuery);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/model/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Closure;
use think\db\BaseQuery as Query;
use think\db\exception\DbException as Exception;
use think\model\Collection;
use think\model\contract\Modelable as Model;

/**
Expand Down Expand Up @@ -159,11 +160,10 @@ public function isSelfRelation(): bool
* 封装关联数据集.
*
* @param array $resultSet 数据集
* @param Model $parent 父模型
*
* @return mixed
* @return Collection
*/
protected function resultSetBuild(array $resultSet, ?Model $parent = null)
protected function resultSetBuild(array $resultSet)
{
return (new $this->model())->toCollection($resultSet);
}
Expand Down
21 changes: 10 additions & 11 deletions src/model/concern/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use think\db\Express;
use think\db\Raw;
use think\helper\Str;
use think\model\Collection;
use think\model\contract\EnumTransform;
use think\model\contract\FieldTypeTransform;
use think\model\contract\Typeable;
Expand Down Expand Up @@ -461,24 +462,22 @@ public function set(string $name, $value)
if (is_null($value) && is_subclass_of($type, Model::class)) {
// 关联数据为空 设置一个空模型
$value = new $type();
} elseif (!($value instanceof Model || $value instanceof Collection)) {
} elseif (!($value instanceof Model || $value instanceof Collection) && !$this->hasSetAttr($name)) {
// 类型自动转换
if (!$this->hasSetAttr($name)) {
$value = $this->readTransform($value, $type);
}
$value = $this->readTransform($value, $type);
}

$this->setData($name, $value);
return $this;
}

protected function hasGetAttr(string $name): bool
{
$attr = Str::studly($name);
$method = 'get' . $attr . 'Attr';
return method_exists($this, $method);
}

/**
* 字段是否定义修改器
*
* @param string $name 名称
*
* @return bool
*/
protected function hasSetAttr(string $name): bool
{
$attr = Str::studly($name);
Expand Down
2 changes: 1 addition & 1 deletion src/model/concern/RelationShip.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ public function bindAttr(string $relation, array $attrs = [])
*
* @return void
*/
public function relationCount(Query $query, array $relations, string $aggregate = 'sum', string $field = '*', bool $useSubQuery = true): void
public function relationCount(Query $query, array $relations, string $aggregate = 'sum', string $field = 'id', bool $useSubQuery = true): void
{
foreach ($relations as $key => $relation) {
$closure = $name = null;
Expand Down
4 changes: 2 additions & 2 deletions src/model/relation/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function getRelation(array $subRelation = [], ?Closure $closure = null)
*
* @return string
*/
public function getRelationCountQuery(?Closure $closure = null, string $aggregate = 'count', string $field = '*', &$name = ''): string
public function getRelationCountQuery(?Closure $closure = null, string $aggregate = 'count', string $field = 'id', &$name = ''): string
{
if ($closure) {
$closure($this->query, $name);
Expand All @@ -112,7 +112,7 @@ public function getRelationCountQuery(?Closure $closure = null, string $aggregat
*
* @return int
*/
public function relationCount(Model $result, ?Closure $closure = null, string $aggregate = 'count', string $field = '*', ? string &$name = null)
public function relationCount(Model $result, ?Closure $closure = null, string $aggregate = 'count', string $field = 'id', ? string &$name = null)
{
$foreignKey = $this->foreignKey;

Expand Down
8 changes: 4 additions & 4 deletions src/model/relation/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub
$data[$result->$pk] = [];
}

$result->setRelation($relation, $this->resultSetBuild($data[$result->$pk], clone $this->parent));
$result->setRelation($relation, $this->resultSetBuild($data[$result->$pk]));
}
}
}
Expand Down Expand Up @@ -330,7 +330,7 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio
$data[$pk] = [];
}

$result->setRelation($relation, $this->resultSetBuild($data[$pk], clone $this->parent));
$result->setRelation($relation, $this->resultSetBuild($data[$pk]));
}
}

Expand All @@ -345,7 +345,7 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio
*
* @return int
*/
public function relationCount(Model $result, ?Closure $closure = null, string $aggregate = 'count', string $field = '*', ?string &$name = null)
public function relationCount(Model $result, ?Closure $closure = null, string $aggregate = 'count', string $field = 'id', ?string &$name = null)
{
$pk = $result->getPk();

Expand Down Expand Up @@ -374,7 +374,7 @@ public function relationCount(Model $result, ?Closure $closure = null, string $a
*
* @return string
*/
public function getRelationCountQuery(?Closure $closure = null, string $aggregate = 'count', string $field = '*', ?string &$name = null): string
public function getRelationCountQuery(?Closure $closure = null, string $aggregate = 'count', string $field = 'id', ?string &$name = null): string
{
if ($closure) {
$closure($this->query, $name);
Expand Down
8 changes: 4 additions & 4 deletions src/model/relation/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub
$data[$pk] = [];
}

$result->setRelation($relation, $this->resultSetBuild($data[$pk], clone $this->parent));
$result->setRelation($relation, $this->resultSetBuild($data[$pk]));
}
}
}
Expand Down Expand Up @@ -131,7 +131,7 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio
$data[$pk] = [];
}

$result->setRelation($relation, $this->resultSetBuild($data[$pk], clone $this->parent));
$result->setRelation($relation, $this->resultSetBuild($data[$pk]));
}
}

Expand All @@ -146,7 +146,7 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio
*
* @return int
*/
public function relationCount(Model $result, ?Closure $closure = null, string $aggregate = 'count', string $field = '*', ?string &$name = null)
public function relationCount(Model $result, ?Closure $closure = null, string $aggregate = 'count', string $field = 'id', ?string &$name = null)
{
$localKey = $this->localKey;

Expand All @@ -173,7 +173,7 @@ public function relationCount(Model $result, ?Closure $closure = null, string $a
*
* @return string
*/
public function getRelationCountQuery(?Closure $closure = null, string $aggregate = 'count', string $field = '*', ?string &$name = null): string
public function getRelationCountQuery(?Closure $closure = null, string $aggregate = 'count', string $field = 'id', ?string &$name = null): string
{
if ($closure) {
$closure($this->query, $name);
Expand Down
8 changes: 4 additions & 4 deletions src/model/relation/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub
}

// 设置关联属性
$result->setRelation($relation, $this->resultSetBuild($data[$pk], clone $this->parent));
$result->setRelation($relation, $this->resultSetBuild($data[$pk]));
}
}
}
Expand Down Expand Up @@ -240,7 +240,7 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio
$data[$pk] = [];
}

$result->setRelation($relation, $this->resultSetBuild($data[$pk], clone $this->parent));
$result->setRelation($relation, $this->resultSetBuild($data[$pk]));
}

/**
Expand Down Expand Up @@ -309,7 +309,7 @@ protected function eagerlyWhere(array $where, string $key, array $subRelation =
*
* @return mixed
*/
public function relationCount(Model $result, ?Closure $closure = null, string $aggregate = 'count', string $field = '*', ?string &$name = null)
public function relationCount(Model $result, ?Closure $closure = null, string $aggregate = 'count', string $field = 'id', ?string &$name = null)
{
$localKey = $this->localKey;

Expand Down Expand Up @@ -349,7 +349,7 @@ public function relationCount(Model $result, ?Closure $closure = null, string $a
*
* @return string
*/
public function getRelationCountQuery(?Closure $closure = null, string $aggregate = 'count', string $field = '*', ?string &$name = null): string
public function getRelationCountQuery(?Closure $closure = null, string $aggregate = 'count', string $field = 'id', ?string &$name = null): string
{
if ($closure) {
$closure($this->query, $name);
Expand Down
4 changes: 2 additions & 2 deletions src/model/relation/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function getRelation(array $subRelation = [], ?Closure $closure = null)
*
* @return string
*/
public function getRelationCountQuery(?Closure $closure = null, string $aggregate = 'count', string $field = '*', ? string &$name = null) : string
public function getRelationCountQuery(?Closure $closure = null, string $aggregate = 'count', string $field = 'id', ? string &$name = null) : string
{
if ($closure) {
$closure($this->query, $name);
Expand All @@ -112,7 +112,7 @@ public function getRelationCountQuery(?Closure $closure = null, string $aggregat
*
* @return int
*/
public function relationCount(Model $result, ?Closure $closure = null, string $aggregate = 'count', string $field = '*', ? string &$name = null)
public function relationCount(Model $result, ?Closure $closure = null, string $aggregate = 'count', string $field = 'id', ? string &$name = null)
{
$localKey = $this->localKey;

Expand Down
8 changes: 4 additions & 4 deletions src/model/relation/MorphMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub
$data[$result->$pk] = [];
}

$result->setRelation($relation, $this->resultSetBuild($data[$result->$pk], clone $this->parent));
$result->setRelation($relation, $this->resultSetBuild($data[$result->$pk]));
}
}
}
Expand Down Expand Up @@ -184,7 +184,7 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio
$data[$key] = [];
}

$result->setRelation($relation, $this->resultSetBuild($data[$key], clone $this->parent));
$result->setRelation($relation, $this->resultSetBuild($data[$key]));
}
}

Expand All @@ -199,7 +199,7 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio
*
* @return mixed
*/
public function relationCount(Model $result, ?Closure $closure = null, string $aggregate = 'count', string $field = '*', ?string &$name = null)
public function relationCount(Model $result, ?Closure $closure = null, string $aggregate = 'count', string $field = 'id', ?string &$name = null)
{
$pk = $result->getPk();

Expand Down Expand Up @@ -229,7 +229,7 @@ public function relationCount(Model $result, ?Closure $closure = null, string $a
*
* @return string
*/
public function getRelationCountQuery(?Closure $closure = null, string $aggregate = 'count', string $field = '*', ?string &$name = null): string
public function getRelationCountQuery(?Closure $closure = null, string $aggregate = 'count', string $field = 'id', ?string &$name = null): string
{
if ($closure) {
$closure($this->query, $name);
Expand Down
4 changes: 2 additions & 2 deletions src/model/relation/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub
$data[$result->$pk] = [];
}

$result->setRelation($relation, $this->resultSetBuild($data[$result->$pk], clone $this->parent));
$result->setRelation($relation, $this->resultSetBuild($data[$result->$pk]));
}
}
}
Expand Down Expand Up @@ -148,7 +148,7 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio
$data[$pk] = [];
}

$result->setRelation($relation, $this->resultSetBuild($data[$pk], clone $this->parent));
$result->setRelation($relation, $this->resultSetBuild($data[$pk]));
}
}

Expand Down

0 comments on commit b1dd14d

Please # to comment.