Skip to content

Commit 7bc5834

Browse files
committed
改进数据集的关联预载入查询 使用cursor查询 减少内存开销
1 parent cd830b9 commit 7bc5834

8 files changed

Lines changed: 20 additions & 20 deletions

File tree

src/Model.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,10 @@ private function getWeakData(string $key, string $name, $default = null)
240240
* 创建新的模型实例.
241241
*
242242
* @param array|object $data
243-
* @param array $options
244243
*
245244
* @return Model|Entity
246245
*/
247-
public function newInstance(array | object $data = [], array $options = [])
246+
public function newInstance(array | object $data = [])
248247
{
249248
$model = new static($data);
250249
if (!empty($data)) {

src/db/PDOConnection.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -653,20 +653,19 @@ public function getPdo()
653653
* @param BaseQuery $query 查询对象
654654
* @param string $sql sql指令
655655
* @param Model|null $model 模型对象实例
656-
* @param null $condition 查询条件
657656
*
658657
* @throws DbException
659658
*
660659
* @return \Generator
661660
*/
662-
public function getCursor(BaseQuery $query, string $sql, $model = null, $condition = null)
661+
public function getCursor(BaseQuery $query, string $sql, $model = null)
663662
{
664663
$this->queryPDOStatement($query, $sql);
665664

666665
// 返回结果集
667666
while ($result = $this->PDOStatement->fetch($this->fetchType)) {
668667
if ($model) {
669-
yield $model->newInstance($result, $condition);
668+
yield $model->newInstance($result);
670669
} else {
671670
yield $result;
672671
}
@@ -966,10 +965,8 @@ public function cursor(BaseQuery $query)
966965
// 生成查询SQL
967966
$sql = $this->builder->select($query);
968967

969-
$condition = $options['where']['AND'] ?? null;
970-
971968
// 执行查询操作
972-
return $this->getCursor($query, $sql, $query->getModel(), $condition);
969+
return $this->getCursor($query, $sql, $query->getModel());
973970
}
974971

975972
/**

src/db/concern/ModelRelationQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ protected function resultToModel(array &$result): void
663663
}
664664
}
665665

666-
$result = $this->model->newInstance($result, $this->options);
666+
$result = $this->model->newInstance($result);
667667

668668
if ($this->suffix) {
669669
$result->setSuffix($this->suffix);

src/model/relation/BelongsToMany.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,11 @@ protected function eagerlyManyToMany(array $where, array $subRelation = [], ?Clo
443443
}
444444

445445
// 预载入关联查询 支持嵌套预载入
446-
$list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where)
446+
$method = ($subRelation || !empty($cache)) ? 'select' : 'cursor';
447+
$list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where)
447448
->with($subRelation)
448449
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
449-
->select();
450+
->$method();
450451

451452
// 组装模型数据
452453
$data = [];

src/model/relation/HasMany.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,12 @@ protected function eagerlyOneToMany(array $where, array $subRelation = [], ?Clos
217217
$this->query->removeOption('limit');
218218
}
219219

220-
$list = $this->query
220+
$method = ($subRelation || !empty($cache)) ? 'select' : 'cursor';
221+
$list = $this->query
221222
->where($where)
222223
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
223224
->with($subRelation)
224-
->select();
225+
->$method();
225226

226227
// 组装模型数据
227228
$data = [];

src/model/relation/HasManyThrough.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ protected function eagerlyWhere(array $where, string $key, array $subRelation =
257257
if ($withLimit && $collection) {
258258
$this->query->removeOption('limit');
259259
}
260-
261-
$list = $this->query
260+
$method = ($subRelation || !empty($cache)) ? 'select' : 'cursor';
261+
$list = $this->query
262262
->where($throughKey, 'in', $keys)
263263
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
264-
->select();
264+
->$method();
265265

266266
// 组装模型数据
267267
$data = [];

src/model/relation/MorphMany.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,12 @@ protected function eagerlyMorphToMany(array $where, array $subRelation = [], ?Cl
298298
$this->query->removeOption('limit');
299299
}
300300

301-
$list = $this->query
301+
$method = ($subRelation || !empty($cache)) ? 'select' : 'cursor';
302+
$list = $this->query
302303
->where($where)
303304
->with($subRelation)
304305
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
305-
->select();
306+
->$method();
306307
$morphKey = $this->morphKey;
307308

308309
// 组装模型数据

src/model/relation/MorphToMany.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,11 @@ protected function eagerlyManyToMany(array $where, array $subRelation = [], ?Clo
256256
}
257257

258258
// 预载入关联查询 支持嵌套预载入
259-
$list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where)
259+
$method = ($subRelation || !empty($cache)) ? 'select' : 'cursor';
260+
$list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where)
260261
->with($subRelation)
261262
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
262-
->select();
263+
->$method();
263264

264265
// 组装模型数据
265266
$data = [];

0 commit comments

Comments
 (0)