From 091ad5e023c15fcce4ceaea2f3814bdf71045cde Mon Sep 17 00:00:00 2001 From: ThinkPHP Date: Thu, 6 Jan 2022 10:48:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/BaseQuery.php | 33 +------- src/db/concern/ModelRelationQuery.php | 105 +++++++++++++++----------- src/db/concern/ResultOperation.php | 23 ++++-- 3 files changed, 81 insertions(+), 80 deletions(-) diff --git a/src/db/BaseQuery.php b/src/db/BaseQuery.php index 50761207..cccd8ef8 100644 --- a/src/db/BaseQuery.php +++ b/src/db/BaseQuery.php @@ -263,14 +263,7 @@ public function value(string $field, $default = null) $result = $this->connection->value($this, $field, $default); $array[$field] = $result; - - if (!empty($this->options['json'])) { - $this->jsonResult($array, $this->options['json']); - } - - if (!empty($this->options['with_attr'])) { - $array = $this->getResultAttr($array, $this->options['with_attr']); - } + $this->result($array); return $array[$field]; } @@ -287,15 +280,7 @@ public function column($field, string $key = ''): array $result = $this->connection->column($this, $field, $key); if (count($result) != count($result, 1)) { - foreach ($result as &$val) { - if (!empty($this->options['json'])) { - $this->jsonResult($val, $this->options['json']); - } - - if (!empty($this->options['with_attr'])) { - $val = $this->getResultAttr($val, $this->options['with_attr']); - } - } + $this->resultSet($result, false); } return $result; @@ -887,19 +872,7 @@ public function json(array $json = [], bool $assoc = false) $this->options['json'] = $json; $this->options['json_assoc'] = $assoc; - if ($this->model) { - return $this->filter(function ($result) use ($json, $assoc) { - if (!empty($json)) { - $this->jsonModelResult($result, $json, $assoc); - } - }); - } - - return $this->filter(function ($result) use ($json) { - if (!empty($json)) { - $this->jsonResult($result, $json); - } - }); + return $this; } /** diff --git a/src/db/concern/ModelRelationQuery.php b/src/db/concern/ModelRelationQuery.php index ae1aea42..0015a24a 100644 --- a/src/db/concern/ModelRelationQuery.php +++ b/src/db/concern/ModelRelationQuery.php @@ -137,9 +137,8 @@ public function relation(array $relation) return $this; } - return $this->filter(function ($result, $options) use ($relation) { - $result->relationQuery($relation, $this->options['with_relation_attr']); - }); + $this->options['relation'] = $relation; + return $this; } /** @@ -206,15 +205,7 @@ public function withAttr($name, callable $callback = null) } } - if (empty($this->model)) { - return $this->filter(function ($result) { - return $this->getResultAttr($result, $this->options['with_attr']); - }, 'with_attr'); - } - - return $this->filter(function ($result) { - $result->withAttr($this->options['with_attr']); - }, 'with_attr'); + return $this; } /** @@ -229,14 +220,8 @@ public function with($with) return $this; } - $with = (array) $with; - - $this->options['with'] = $with; - return $this->filter(function ($result) use ($with) { - if (empty($this->options['is_resultSet'])) { - $result->eagerlyResult($with, $this->options['with_relation_attr'], false, $this->options['with_cache'] ?? false); - } - }, 'with'); + $this->options['with'] = (array) $with; + return $this; } /** @@ -282,12 +267,7 @@ public function withJoin($with, string $joinType = '') $this->via(); $this->options['with_join'] = $with; - return $this->filter(function ($result) use ($with) { - // JOIN预载入查询 - if (empty($this->options['is_resultSet'])) { - $result->eagerlyResult($with, $this->options['with_relation_attr'], true, $this->options['with_cache'] ?? false); - } - }, 'with_join'); + return $this; } /** @@ -306,12 +286,8 @@ protected function withAggregate($relations, string $aggregate = 'count', $field } if (!$subQuery) { - $this->options['with_aggregate'][] = [$relations, $aggregate, $field]; - return $this->filter(function ($result) use ($withAggregate) { - foreach ($this->options['with_aggregate'] as $val) { - $result->relationCount($this, (array) $val[0], $val[1], $val[2], false); - } - }); + $this->options['with_aggregate'][] = [(array) $relations, $aggregate, $field]; + return $this; } if (!isset($this->options['field'])) { @@ -459,20 +435,18 @@ public function hasWhere(string $relation, $where = [], string $fields = '*', st /** * JSON字段数据转换 * @access protected - * @param Model $result 查询数据 - * @param array $json JSON字段 - * @param bool $assoc 是否转换为数组 + * @param array $result 查询数据 * @return void */ - protected function jsonModelResult(Model $result, array $json = [], bool $assoc = false): void + protected function jsonModelResult(array &$result): void { $withAttr = $this->options['with_attr']; - foreach ($json as $name) { - if (!isset($result->$name)) { + foreach ($this->options['json'] as $name) { + if (!isset($result[$name])) { continue; } - $jsonData = json_decode($result->getData($name), true); + $jsonData = json_decode($result[$name], true); if (isset($withAttr[$name])) { foreach ($withAttr[$name] as $key => $closure) { @@ -480,7 +454,7 @@ protected function jsonModelResult(Model $result, array $json = [], bool $assoc } } - $result->set($name, !$assoc ? (object) $jsonData : $jsonData); + $result[$name] = !$this->options['json_assoc'] ? (object) $jsonData : $jsonData; } } @@ -497,6 +471,7 @@ protected function resultSetToModelCollection(array $resultSet): ModelCollection } $this->options['is_resultSet'] = true; + foreach ($resultSet as $key => &$result) { // 数据转换为模型对象 $this->resultToModel($result); @@ -505,7 +480,13 @@ protected function resultSetToModelCollection(array $resultSet): ModelCollection foreach (['with', 'with_join'] as $with) { // 关联预载入 if (!empty($this->options[$with])) { - $result->eagerlyResultSet($resultSet, $this->options[$with], $this->options['with_relation_attr'], false, $this->options['with_cache'] ?? false); + $result->eagerlyResultSet( + $resultSet, + $this->options[$with], + $this->options['with_relation_attr'], + 'with_join' == $with ? true : false, + $this->options['with_cache'] ?? false + ); } } @@ -521,13 +502,53 @@ protected function resultSetToModelCollection(array $resultSet): ModelCollection */ protected function resultToModel(array &$result): void { - $result = $this->model->newInstance($result, !empty($this->options['is_resultSet']) ? null : $this->getModelUpdateCondition($this->options), $this->options); + // JSON数据处理 + if (!empty($this->options['json'])) { + $this->jsonModelResult($result); + } + + $result = $this->model->newInstance( + $result, + !empty($this->options['is_resultSet']) ? null : $this->getModelUpdateCondition($this->options), + $this->options + ); // 模型数据处理 foreach ($this->options['filter'] as $filter) { call_user_func_array($filter, [$result, $this->options]); } + // 关联查询 + if (!empty($this->options['relation'])) { + $result->relationQuery($this->options['relation'], $this->options['with_relation_attr']); + } + + // 关联预载入查询 + if (empty($this->options['is_resultSet'])) { + foreach (['with', 'with_join'] as $with) { + if (!empty($this->options[$with])) { + $result->eagerlyResult( + $this->options[$with], + $this->options['with_relation_attr'], + 'with_join' == $with ? true : false, + $this->options['with_cache'] ?? false + ); + } + } + } + + // 关联统计查询 + if (!empty($this->options['with_aggregate'])) { + foreach ($this->options['with_aggregate'] as $val) { + $result->relationCount($this, $val[0], $val[1], $val[2], false); + } + } + + // 动态获取器 + if (!empty($this->options['with_attr'])) { + $result->withAttr($this->options['with_attr']); + } + foreach (['hidden', 'visible', 'append'] as $name) { if (!empty($this->options[$name])) { $result->$name($this->options[$name]); diff --git a/src/db/concern/ResultOperation.php b/src/db/concern/ResultOperation.php index 2a6f1477..ea269163 100644 --- a/src/db/concern/ResultOperation.php +++ b/src/db/concern/ResultOperation.php @@ -75,10 +75,20 @@ public function failException(bool $fail = true) */ protected function result(array &$result): void { + // JSON数据处理 + if (!empty($this->options['json'])) { + $this->jsonResult($result); + } + // 查询数据处理 foreach ($this->options['filter'] as $filter) { $result = call_user_func_array($filter, [$result, $this->options]); } + + // 获取器 + if (!empty($this->options['with_attr'])) { + $this->getResultAttr($result, $this->options['with_attr']); + } } /** @@ -105,9 +115,9 @@ protected function resultSet(array &$resultSet, bool $toCollection = true): void * @access protected * @param array $result 查询数据 * @param array $withAttr 字段获取器 - * @return array + * @return void */ - protected function getResultAttr(array $result, array $withAttr = []): array + protected function getResultAttr(array &$result, array $withAttr = []): void { foreach ($withAttr as $name => $closure) { $name = Str::snake($name); @@ -123,8 +133,6 @@ protected function getResultAttr(array $result, array $withAttr = []): array $result[$name] = $closure($result[$name] ?? null, $result); } } - - return $result; } /** @@ -158,13 +166,12 @@ public function findOrEmpty($data = null) /** * JSON字段数据转换 * @access protected - * @param array $result 查询数据 - * @param array $json JSON字段 + * @param array $result 查询数据 * @return void */ - protected function jsonResult(array &$result, array $json = []): void + protected function jsonResult(array &$result): void { - foreach ($json as $name) { + foreach ($this->options['json'] as $name) { if (!isset($result[$name])) { continue; }