Skip to content

Commit fae382c

Browse files
committed
改进LazyCollection 增加单元测试
1 parent b35971c commit fae382c

File tree

4 files changed

+767
-189
lines changed

4 files changed

+767
-189
lines changed

src/model/Collection.php

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

1616
use think\Collection as BaseCollection;
17-
use think\model\concern\ModelCollection;
1817
use think\model\contract\Modelable as Model;
1918
use think\Paginator;
2019

@@ -28,7 +27,6 @@
2827
*/
2928
class Collection extends BaseCollection
3029
{
31-
use ModelCollection;
3230
/**
3331
* 延迟预载入关联查询.
3432
*
@@ -47,6 +45,174 @@ public function load(array $relation, $cache = false)
4745
return $this;
4846
}
4947

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

src/model/LazyCollection.php

Lines changed: 174 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@
1414
namespace think\model;
1515

1616
use think\db\LazyCollection as DbCollection;
17-
use think\model\concern\ModelCollection;
17+
use think\model\contract\Modelable as Model;
1818

1919
/**
2020
* 模型惰性集合类 - 用于高效处理大数据集.
21-
*
2221
*/
2322
class LazyCollection extends DbCollection
2423
{
25-
use ModelCollection;
26-
2724
/**
2825
* 延迟预载入关联查询
2926
* @param array $relation 关联
@@ -93,4 +90,177 @@ public function sort(?callable $callback = null): Collection
9390
}
9491
return new Collection($items);
9592
}
93+
94+
/**
95+
* 转换为视图模型
96+
*
97+
* @param string $view 视图类名
98+
*
99+
* @return $this
100+
*/
101+
public function toView(string $view)
102+
{
103+
return $this->map(function (Model $model) use($view) {
104+
return $model->toView($view);
105+
});
106+
}
107+
108+
/**
109+
* 删除数据集的数据.
110+
*
111+
* @return bool
112+
*/
113+
public function delete(): bool
114+
{
115+
$this->each(function (Model $model) {
116+
$model->delete();
117+
});
118+
119+
return true;
120+
}
121+
122+
/**
123+
* 更新数据.
124+
*
125+
* @param array $data 数据数组
126+
* @param array $allowField 允许字段
127+
*
128+
* @return bool
129+
*/
130+
public function update(array $data, array $allowField = []): bool
131+
{
132+
$this->each(function (Model $model) use ($data, $allowField) {
133+
if (!empty($allowField)) {
134+
$model->allowField($allowField);
135+
}
136+
137+
$model->save($data);
138+
});
139+
140+
return true;
141+
}
142+
143+
/**
144+
* 设置需要隐藏的输出属性.
145+
*
146+
* @param array $hidden 属性列表
147+
* @param bool $merge 是否合并
148+
*
149+
* @return static
150+
*/
151+
public function hidden(array $hidden, bool $merge = false)
152+
{
153+
return new static(function () use ($hidden, $merge) {
154+
foreach ($this->getIterator() as $key => $model) {
155+
$model->hidden($hidden, $merge);
156+
yield $key => $model;
157+
}
158+
});
159+
}
160+
161+
/**
162+
* 设置需要输出的属性.
163+
*
164+
* @param array $visible
165+
* @param bool $merge 是否合并
166+
*
167+
* @return static
168+
*/
169+
public function visible(array $visible, bool $merge = false)
170+
{
171+
return new static(function () use ($visible, $merge) {
172+
foreach ($this->getIterator() as $key => $model) {
173+
$model->visible($visible, $merge);
174+
yield $key => $model;
175+
}
176+
});
177+
}
178+
179+
/**
180+
* 设置需要追加的输出属性.
181+
*
182+
* @param array $append 属性列表
183+
* @param bool $merge 是否合并
184+
*
185+
* @return static
186+
*/
187+
public function append(array $append, bool $merge = false)
188+
{
189+
return new static(function () use ($append, $merge) {
190+
foreach ($this->getIterator() as $key => $model) {
191+
$model->append($append, $merge);
192+
yield $key => $model;
193+
}
194+
});
195+
}
196+
197+
/**
198+
* 设置属性映射.
199+
*
200+
* @param array $mapping 属性映射
201+
*
202+
* @return static
203+
*/
204+
public function mapping(array $mapping)
205+
{
206+
return new static(function () use ($mapping) {
207+
foreach ($this->getIterator() as $key => $model) {
208+
$model->mapping($mapping);
209+
yield $key => $model;
210+
}
211+
});
212+
}
213+
214+
/**
215+
* 设置模型输出场景.
216+
*
217+
* @param string $scene 场景名称
218+
*
219+
* @return static
220+
*/
221+
public function scene(string $scene)
222+
{
223+
return new static(function () use ($scene) {
224+
foreach ($this->getIterator() as $key => $model) {
225+
$model->scene($scene);
226+
yield $key => $model;
227+
}
228+
});
229+
}
230+
231+
/**
232+
* 设置数据字段获取器.
233+
*
234+
* @param string|array $name 字段名
235+
* @param callable $callback 闭包获取器
236+
*
237+
* @return static
238+
*/
239+
public function withAttr(string|array $name, ?callable $callback = null)
240+
{
241+
return new static(function () use ($name, $callback) {
242+
foreach ($this->getIterator() as $key => $model) {
243+
$model->withFieldAttr($name, $callback);
244+
yield $key => $model;
245+
}
246+
});
247+
}
248+
249+
/**
250+
* 绑定(一对一)关联属性到当前模型.
251+
*
252+
* @param string $relation 关联名称
253+
* @param array $attrs 绑定属性
254+
*
255+
* @return static
256+
*/
257+
public function bindAttr(string $relation, array $attrs = [])
258+
{
259+
return new static(function () use ($relation, $attrs) {
260+
foreach ($this->getIterator() as $key => $model) {
261+
$model->bindAttr($relation, $attrs);
262+
yield $key => $model;
263+
}
264+
});
265+
}
96266
}

0 commit comments

Comments
 (0)