Skip to content

Commit b35971c

Browse files
committed
调整lazyCollection类并完善单元测试
1 parent 3d559b6 commit b35971c

File tree

9 files changed

+1264
-719
lines changed

9 files changed

+1264
-719
lines changed

src/db/LazyCollection.php

Lines changed: 580 additions & 0 deletions
Large diffs are not rendered by default.

src/db/Query.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use PDOStatement;
1717
use think\db\exception\DbException as Exception;
18-
use think\model\LazyCollection;
18+
use think\model\LazyCollection as ModelLazyCollection;
1919

2020
/**
2121
* PDO数据查询类.
@@ -503,7 +503,7 @@ public function getPdo(): PDOStatement
503503
}
504504

505505
/**
506-
* 使用游标查找记录.
506+
* 使用游标查找记录.(不支持关联查询和查询缓存)
507507
*
508508
* @param bool $unbuffered 是否开启无缓冲查询(仅限mysql)
509509
*
@@ -513,13 +513,14 @@ public function cursor(bool $unbuffered = false): LazyCollection
513513
{
514514
$connection = clone $this->connection;
515515

516-
return new LazyCollection(function () use ($connection, $unbuffered) {
516+
$class = $this->model ? ModelLazyCollection::class : LazyCollection::class;
517+
return new $class(function () use ($connection, $unbuffered) {
517518
yield from $connection->cursor($this, $unbuffered);
518519
});
519520
}
520521

521522
/**
522-
* 流式处理查询结果
523+
* 流式处理查询结果(不支持关联查询和查询缓存)
523524
*
524525
* @param callable $callback 处理回调
525526
* @param bool $unbuffered 是否使用无缓冲查询(仅MySQL支持)
@@ -618,8 +619,8 @@ public function lazy(int $count = 1000, ?string $column = null, string $order =
618619
if ($count < 1) {
619620
throw new Exception('The chunk size should be at least 1');
620621
}
621-
622-
return new LazyCollection(function () use ($count, $column, $order) {
622+
$class = $this->model ? ModelLazyCollection::class : LazyCollection::class;
623+
return new $class(function () use ($count, $column, $order) {
623624
$limit = (int) $this->getOption('limit', 0);
624625
$column = $column ?: $this->getPk();
625626
$length = $limit && $count >= $limit ? $limit : $count;

src/model/Collection.php

Lines changed: 2 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace think\model;
1515

1616
use think\Collection as BaseCollection;
17+
use think\model\concern\ModelCollection;
1718
use think\model\contract\Modelable as Model;
1819
use think\Paginator;
1920

@@ -27,13 +28,7 @@
2728
*/
2829
class Collection extends BaseCollection
2930
{
30-
public function toView(string $view)
31-
{
32-
return $this->map(function (Model $model) use($view) {
33-
return $model->toView($view);
34-
});
35-
}
36-
31+
use ModelCollection;
3732
/**
3833
* 延迟预载入关联查询.
3934
*
@@ -52,160 +47,6 @@ public function load(array $relation, $cache = false)
5247
return $this;
5348
}
5449

55-
/**
56-
* 删除数据集的数据.
57-
*
58-
* @return bool
59-
*/
60-
public function delete(): bool
61-
{
62-
$this->each(function (Model $model) {
63-
$model->delete();
64-
});
65-
66-
return true;
67-
}
68-
69-
/**
70-
* 更新数据.
71-
*
72-
* @param array $data 数据数组
73-
* @param array $allowField 允许字段
74-
*
75-
* @return bool
76-
*/
77-
public function update(array $data, array $allowField = []): bool
78-
{
79-
$this->each(function (Model $model) use ($data, $allowField) {
80-
if (!empty($allowField)) {
81-
$model->allowField($allowField);
82-
}
83-
84-
$model->save($data);
85-
});
86-
87-
return true;
88-
}
89-
90-
/**
91-
* 设置需要隐藏的输出属性.
92-
*
93-
* @param array $hidden 属性列表
94-
* @param bool $merge 是否合并
95-
*
96-
* @return $this
97-
*/
98-
public function hidden(array $hidden, bool $merge = false)
99-
{
100-
$this->each(function (Model $model) use ($hidden, $merge) {
101-
$model->hidden($hidden, $merge);
102-
});
103-
104-
return $this;
105-
}
106-
107-
/**
108-
* 设置需要输出的属性.
109-
*
110-
* @param array $visible
111-
* @param bool $merge 是否合并
112-
*
113-
* @return $this
114-
*/
115-
public function visible(array $visible, bool $merge = false)
116-
{
117-
$this->each(function (Model $model) use ($visible, $merge) {
118-
$model->visible($visible, $merge);
119-
});
120-
121-
return $this;
122-
}
123-
124-
/**
125-
* 设置需要追加的输出属性.
126-
*
127-
* @param array $append 属性列表
128-
* @param bool $merge 是否合并
129-
*
130-
* @return $this
131-
*/
132-
public function append(array $append, bool $merge = false)
133-
{
134-
$this->each(function (Model $model) use ($append, $merge) {
135-
$model->append($append, $merge);
136-
});
137-
138-
return $this;
139-
}
140-
141-
/**
142-
* 设置属性映射.
143-
*
144-
* @param array $mapping 属性映射
145-
*
146-
* @return $this
147-
*/
148-
public function mapping(array $mapping)
149-
{
150-
$this->each(function (Model $model) use ($mapping) {
151-
$model->mapping($mapping);
152-
});
153-
154-
return $this;
155-
}
156-
157-
/**
158-
* 设置模型输出场景.
159-
*
160-
* @param string $scene 场景名称
161-
*
162-
* @return $this
163-
*/
164-
public function scene(string $scene)
165-
{
166-
$this->each(function (Model $model) use ($scene) {
167-
$model->scene($scene);
168-
});
169-
170-
return $this;
171-
}
172-
173-
/**
174-
* 设置数据字段获取器.
175-
*
176-
* @param string|array $name 字段名
177-
* @param callable $callback 闭包获取器
178-
*
179-
* @return $this
180-
*/
181-
public function withAttr(string|array $name, ?callable $callback = null)
182-
{
183-
$this->each(function (Model $model) use ($name, $callback) {
184-
$model->withFieldAttr($name, $callback);
185-
});
186-
187-
return $this;
188-
}
189-
190-
/**
191-
* 绑定(一对一)关联属性到当前模型.
192-
*
193-
* @param string $relation 关联名称
194-
* @param array $attrs 绑定属性
195-
*
196-
* @throws Exception
197-
*
198-
* @return $this
199-
*/
200-
public function bindAttr(string $relation, array $attrs = [])
201-
{
202-
$this->each(function (Model $model) use ($relation, $attrs) {
203-
$model->bindAttr($relation, $attrs);
204-
});
205-
206-
return $this;
207-
}
208-
20950
/**
21051
* 按指定键整理数据.
21152
*

0 commit comments

Comments
 (0)