chengkun
2025-09-11 364a083e94138f7ed2d8114bf6dbdfda4eaf2683
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
 
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zhangyajun <448901948@qq.com>
// +----------------------------------------------------------------------
declare(strict_types=1);
 
namespace think\model;
 
use think\Collection as BaseCollection;
use think\Model;
use think\Paginator;
 
/**
 * 模型数据集类.
 *
 * @template TKey of array-key
 * @template TModel of \think\Model
 *
 * @extends BaseCollection<TKey, TModel>
 */
class Collection extends BaseCollection
{
    /**
     * 延迟预载入关联查询.
     *
     * @param array $relation 关联
     * @param mixed $cache    关联缓存
     *
     * @return $this
     */
    public function load(array $relation, $cache = false)
    {
        if (!$this->isEmpty()) {
            $item = current($this->items);
            $item->eagerlyResultSet($this->items, $relation, [], false, $cache);
        }
 
        return $this;
    }
 
    /**
     * 删除数据集的数据.
     *
     * @return bool
     */
    public function delete(): bool
    {
        $this->each(function (Model $model) {
            $model->delete();
        });
 
        return true;
    }
 
    /**
     * 更新数据.
     *
     * @param array $data       数据数组
     * @param array $allowField 允许字段
     *
     * @return bool
     */
    public function update(array $data, array $allowField = []): bool
    {
        $this->each(function (Model $model) use ($data, $allowField) {
            if (!empty($allowField)) {
                $model->allowField($allowField);
            }
 
            $model->save($data);
        });
 
        return true;
    }
 
    /**
     * 设置需要隐藏的输出属性.
     *
     * @param array $hidden 属性列表
     * @param bool  $merge  是否合并
     *
     * @return $this
     */
    public function hidden(array $hidden, bool $merge = false)
    {
        $this->each(function (Model $model) use ($hidden, $merge) {
            $model->hidden($hidden, $merge);
        });
 
        return $this;
    }
 
    /**
     * 设置需要输出的属性.
     *
     * @param array $visible
     * @param bool  $merge   是否合并
     *
     * @return $this
     */
    public function visible(array $visible, bool $merge = false)
    {
        $this->each(function (Model $model) use ($visible, $merge) {
            $model->visible($visible, $merge);
        });
 
        return $this;
    }
 
    /**
     * 设置需要追加的输出属性.
     *
     * @param array $append 属性列表
     * @param bool  $merge  是否合并
     *
     * @return $this
     */
    public function append(array $append, bool $merge = false)
    {
        $this->each(function (Model $model) use ($append, $merge) {
            $model->append($append, $merge);
        });
 
        return $this;
    }
 
    /**
     * 设置属性映射.
     *
     * @param array $mapping 属性映射
     *
     * @return $this
     */
    public function mapping(array $mapping)
    {
        $this->each(function (Model $model) use ($mapping) {
            $model->mapping($mapping);
        });
 
        return $this;
    }
 
    /**
     * 设置模型输出场景.
     *
     * @param string $scene 场景名称
     *
     * @return $this
     */
    public function scene(string $scene)
    {
        $this->each(function (Model $model) use ($scene) {
            $model->scene($scene);
        });
 
        return $this;
    }
 
    /**
     * 设置父模型.
     *
     * @param Model $parent 父模型
     *
     * @return $this
     */
    public function setParent(Model $parent)
    {
        $this->each(function (Model $model) use ($parent) {
            $model->setParent($parent);
        });
 
        return $this;
    }
 
    /**
     * 设置数据字段获取器.
     *
     * @param string|array $name     字段名
     * @param callable     $callback 闭包获取器
     *
     * @return $this
     */
    public function withAttr(string|array $name, ?callable $callback = null)
    {
        $this->each(function (Model $model) use ($name, $callback) {
            $model->withFieldAttr($name, $callback);
        });
 
        return $this;
    }
 
    /**
     * 绑定(一对一)关联属性到当前模型.
     *
     * @param string $relation 关联名称
     * @param array  $attrs    绑定属性
     *
     * @throws Exception
     *
     * @return $this
     */
    public function bindAttr(string $relation, array $attrs = [])
    {
        $this->each(function (Model $model) use ($relation, $attrs) {
            $model->bindAttr($relation, $attrs);
        });
 
        return $this;
    }
 
    /**
     * 按指定键整理数据.
     *
     * @param mixed       $items    数据
     * @param string|null $indexKey 键名
     *
     * @return array
     */
    public function dictionary($items = null, ?string &$indexKey = null)
    {
        if ($items instanceof self || $items instanceof Paginator) {
            $items = $items->all();
        }
 
        $items = is_null($items) ? $this->items : $items;
 
        if ($items && empty($indexKey)) {
            $indexKey = $items[0]->getPk();
        }
 
        if (isset($indexKey) && is_string($indexKey)) {
            return array_column($items, null, $indexKey);
        }
 
        return $items;
    }
 
    /**
     * 比较数据集,返回差集.
     *
     * @param mixed       $items    数据
     * @param string|null $indexKey 指定比较的键名
     *
     * @return static
     */
    public function diff($items, ?string $indexKey = null)
    {
        if ($this->isEmpty()) {
            return new static($items);
        }
 
        $diff = [];
        $dictionary = $this->dictionary($items, $indexKey);
 
        if (is_string($indexKey)) {
            foreach ($this->items as $item) {
                if (!isset($dictionary[$item[$indexKey]])) {
                    $diff[] = $item;
                }
            }
        }
 
        return new static($diff);
    }
 
    /**
     * 比较数据集,返回交集.
     *
     * @param mixed       $items    数据
     * @param string|null $indexKey 指定比较的键名
     *
     * @return static
     */
    public function intersect($items, ?string $indexKey = null)
    {
        if ($this->isEmpty()) {
            return new static([]);
        }
 
        $intersect = [];
        $dictionary = $this->dictionary($items, $indexKey);
 
        if (is_string($indexKey)) {
            foreach ($this->items as $item) {
                if (isset($dictionary[$item[$indexKey]])) {
                    $intersect[] = $item;
                }
            }
        }
 
        return new static($intersect);
    }
}