chengkun
2025-06-05 4080b5997b38ca84b3b203c7101dcadb97b76925
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
<?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: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
 
namespace think\db\concern;
 
use Closure;
use think\helper\Str;
use think\Model;
use think\model\Collection as ModelCollection;
 
/**
 * 模型及关联查询.
 */
trait ModelRelationQuery
{
    /**
     * 当前模型对象
     *
     * @var Model
     */
    protected $model;
 
    /**
     * 指定模型.
     *
     * @param Model $model 模型对象实例
     *
     * @return $this
     */
    public function model(Model $model)
    {
        $this->model = $model;
 
        return $this;
    }
 
    /**
     * 获取当前的模型对象
     *
     * @return Model|null
     */
    public function getModel()
    {
        return $this->model;
    }
 
    /**
     * 设置需要隐藏的输出属性.
     *
     * @param array $hidden 属性列表
     * @param bool $merge 是否合并
     *
     * @return $this
     */
    public function hidden(array $hidden, bool $merge = false)
    {
        $this->options['hidden'] = [$hidden, $merge];
 
        return $this;
    }
 
    /**
     * 设置需要输出的属性.
     *
     * @param array $visible 属性列表
     * @param bool  $merge 是否合并
     *
     * @return $this
     */
    public function visible(array $visible, bool $merge = false)
    {
        $this->options['visible'] = [$visible, $merge];
 
        return $this;
    }
 
    /**
     * 设置需要附加的输出属性.
     *
     * @param array $append 属性列表
     * @param bool  $merge  是否合并
     *
     * @return $this
     */
    public function append(array $append, bool $merge = false)
    {
        $this->options['append'] = [$append, $merge];
 
        return $this;
    }
 
    /**
     * 设置模型的输出映射.
     *
     * @param array $mapping 映射列表
     *
     * @return $this
     */
    public function mapping(array $mapping)
    {
        $this->options['mapping'] = $mapping;
 
        return $this;
    }
 
    /**
     * 添加查询范围.
     *
     * @param array|string|Closure $scope 查询范围定义
     * @param array                $args  参数
     *
     * @return $this
     */
    public function scope($scope, ...$args)
    {
        // 查询范围的第一个参数始终是当前查询对象
        array_unshift($args, $this);
 
        if ($scope instanceof Closure) {
            $this->options['scope'][] = [$scope, $args];
            return $this;
        }
 
        if ($this->model) {
            if (is_string($scope)) {
                $scope = explode(',', $scope);
            }
 
            // 检查模型类的查询范围方法
            foreach ($scope as $name) {
                $method = 'scope' . trim($name);
                if (method_exists($this->model, $method)) {
                    $this->options['scope'][$name] = [[$this->model, $method], $args];
                }
            }
        }
 
        return $this;
    }
 
    /**
     * 执行查询范围查询.
     *
     * @return $this
     */
    protected function scopeQuery()
    {
        if (!empty($this->options['scope'])) {
            foreach ($this->options['scope'] as $val) {
                [$call, $args] = $val;
                call_user_func_array($call, $args);
            }
        }
 
        return $this;
    }
 
    /**
     * 指定不使用的查询范围.
     *
     * @param array $scope 查询范围
     *
     * @return $this
     */
    public function withoutScope(array $scope = [])
    {
        if (empty($scope)) {
            $this->options['scope'] = [];
            return $this;
        }
 
        foreach ($scope as $name) {
            if (isset($this->options['scope'][$name])) {
                unset($this->options['scope'][$name]);
            }
        }
 
        return $this;
    }
 
    /**
     * 设置关联查询.
     *
     * @param array $relation 关联名称
     *
     * @return $this
     */
    public function relation(array $relation)
    {
        if (empty($this->model) || empty($relation)) {
            return $this;
        }
 
        $this->options['relation'] = $relation;
 
        return $this;
    }
 
    /**
     * 使用搜索器条件搜索字段.
     *
     * @param string|array $fields 搜索字段
     * @param mixed        $data   搜索数据
     * @param bool         $strict 是否严格检查数据
     *
     * @return $this
     */
    public function withSearch($fields, $data = [], bool $strict = true)
    {
        if (is_string($fields)) {
            $fields = explode(',', $fields);
        }
 
        $likeFields = $this->getConfig('match_like_fields') ?: [];
 
        foreach ($fields as $key => $field) {
            if ($field instanceof Closure) {
                $field($this, $data[$key] ?? null, $data);
            } elseif ($this->model) {
                // 检查字段是否有数据
                if ($strict && (!isset($data[$field]) || (empty($data[$field]) && !in_array($data[$field], ['0', 0])))) {
                    continue;
                }
 
                $fieldName = is_numeric($key) ? $field : $key;
                $method    = 'search' . Str::studly($fieldName) . 'Attr';
                if (method_exists($this->model, $method)) {
                    $this->model->$method($this, $data[$field], $data);
                } elseif (isset($data[$field])) {
                    $this->where($fieldName, in_array($fieldName, $likeFields) ? 'like' : '=', in_array($fieldName, $likeFields) ? '%' . $data[$field] . '%' : $data[$field]);
                }
            }
        }
 
        return $this;
    }
 
    /**
     * 限制关联数据的字段 已废弃直接使用field或withoutfield替代.
     *
     * @deprecated
     *
     * @param array|string $field 关联字段限制
     *
     * @return $this
     */
    public function withField($field)
    {
        return $this->field($field);
    }
 
    /**
     * 限制关联数据的数量 已废弃直接使用limit替代.
     *
     * @deprecated
     *
     * @param int $limit 关联数量限制
     *
     * @return $this
     */
    public function withLimit(int $limit)
    {
        return $this->limit($limit);
    }
 
    /**
     * 设置关联数据不存在的时候默认值
     *
     * @param mixed $data 默认值
     *
     * @return $this
     */
    public function withDefault($data = null)
    {
        $this->options['default_model'] = $data;
 
        return $this;
    }
 
    /**
     * 设置关联模型的动态绑定
     *
     * @param array $attr 绑定数据
     *
     * @return $this
     */
    public function withBind(array $attr)
    {
        $this->options['bind_attr'] = $attr;
        return $this;
    }
 
    /**
     * 设置数据字段获取器.
     *
     * @param string|array $name     字段名
     * @param callable     $callback 闭包获取器
     *
     * @return $this
     */
    public function withAttr(string | array $name, ?callable $callback = null)
    {
        if (is_array($name)) {
            foreach ($name as $key => $val) {
                $this->withAttr($key, $val);
            }
 
            return $this;
        }
 
        $this->options['with_attr'][$name] = $callback;
 
        if (str_contains($name, '.')) {
            [$relation, $field] = explode('.', $name);
 
            if (!empty($this->options['json']) && in_array($relation, $this->options['json'])) {
            } else {
                $this->options['with_relation_attr'][$relation][$field] = $callback;
                unset($this->options['with_attr'][$name]);
            }
        }
 
        return $this;
    }
 
    /**
     * 关联预载入 In方式.
     *
     * @param array|string $with 关联方法名称
     *
     * @return $this
     */
    public function with(array | string $with)
    {
        if (empty($this->model) || empty($with)) {
            return $this;
        }
 
        $this->options['with'] = (array) $with;
 
        return $this;
    }
 
    /**
     * 关联预载入 JOIN方式.
     *
     * @param array|string $with     关联方法名
     * @param string       $joinType JOIN方式
     *
     * @return $this
     */
    public function withJoin(array | string $with, string $joinType = '')
    {
        if (empty($this->model) || empty($with)) {
            return $this;
        }
 
        $with  = (array) $with;
        $first = true;
 
        foreach ($with as $key => $relation) {
            $closure = null;
            $field   = true;
 
            if ($relation instanceof Closure) {
                // 支持闭包查询过滤关联条件
                $closure  = $relation;
                $relation = $key;
            } elseif (is_array($relation)) {
                $field    = $relation;
                $relation = $key;
            } elseif (is_string($relation) && str_contains($relation, '.')) {
                $relation = strstr($relation, '.', true);
            }
 
            $result = $this->model->eagerly($this, $relation, $field, $joinType, $closure, $first);
 
            if (!$result) {
                unset($with[$key]);
            } else {
                $first = false;
            }
        }
 
        $this->via();
        $this->options['with_join'] = $with;
 
        return $this;
    }
 
    /**
     * 关联统计
     *
     * @param array|string $relations 关联方法名
     * @param string       $aggregate 聚合查询方法
     * @param string       $field     字段
     * @param bool         $subQuery  是否使用子查询
     *
     * @return $this
     */
    protected function withAggregate(string | array $relations, string $aggregate = 'count', $field = '*', bool $subQuery = true)
    {
        if (empty($this->model)) {
            return $this;
        }
 
        if (!$subQuery) {
            $this->options['with_aggregate'][] = [(array) $relations, $aggregate, $field];
 
            return $this;
        }
 
        if (!isset($this->options['field'])) {
            $this->field('*');
        }
 
        $this->model->relationCount($this, (array) $relations, $aggregate, $field, true);
 
        return $this;
    }
 
    /**
     * 关联缓存.
     *
     * @param string|array|bool $relation 关联方法名
     * @param mixed             $key      缓存key
     * @param int|\DateTime     $expire   缓存有效期
     * @param string            $tag      缓存标签
     *
     * @return $this
     */
    public function withCache(string | array | bool $relation = true, $key = true, $expire = null, ?string $tag = null)
    {
        if (empty($this->model)) {
            return $this;
        }
 
        if (false === $relation || false === $key || !$this->getConnection()->getCache()) {
            return $this;
        }
 
        if ($key instanceof \DateTimeInterface  || $key instanceof \DateInterval  || (is_int($key) && is_null($expire))) {
            $expire = $key;
            $key    = true;
        }
 
        if (true === $relation || is_numeric($relation)) {
            $this->options['with_cache'] = $relation;
 
            return $this;
        }
 
        $relations = (array) $relation;
        foreach ($relations as $name => $relation) {
            if (!is_numeric($name)) {
                $this->options['with_cache'][$name] = is_array($relation) ? $relation : [$key, $relation, $tag];
            } else {
                $this->options['with_cache'][$relation] = [$key, $expire, $tag];
            }
        }
 
        return $this;
    }
 
    /**
     * 关联统计
     *
     * @param string|array $relation 关联方法名
     * @param bool         $subQuery 是否使用子查询
     *
     * @return $this
     */
    public function withCount(string | array $relation, bool $subQuery = true)
    {
        return $this->withAggregate($relation, 'count', '*', $subQuery);
    }
 
    /**
     * 关联统计Sum.
     *
     * @param string|array $relation 关联方法名
     * @param string       $field    字段
     * @param bool         $subQuery 是否使用子查询
     *
     * @return $this
     */
    public function withSum(string | array $relation, string $field, bool $subQuery = true)
    {
        return $this->withAggregate($relation, 'sum', $field, $subQuery);
    }
 
    /**
     * 关联统计Max.
     *
     * @param string|array $relation 关联方法名
     * @param string       $field    字段
     * @param bool         $subQuery 是否使用子查询
     *
     * @return $this
     */
    public function withMax(string | array $relation, string $field, bool $subQuery = true)
    {
        return $this->withAggregate($relation, 'max', $field, $subQuery);
    }
 
    /**
     * 关联统计Min.
     *
     * @param string|array $relation 关联方法名
     * @param string       $field    字段
     * @param bool         $subQuery 是否使用子查询
     *
     * @return $this
     */
    public function withMin(string | array $relation, string $field, bool $subQuery = true)
    {
        return $this->withAggregate($relation, 'min', $field, $subQuery);
    }
 
    /**
     * 关联统计Avg.
     *
     * @param string|array $relation 关联方法名
     * @param string       $field    字段
     * @param bool         $subQuery 是否使用子查询
     *
     * @return $this
     */
    public function withAvg(string | array $relation, string $field, bool $subQuery = true)
    {
        return $this->withAggregate($relation, 'avg', $field, $subQuery);
    }
 
    /**
     * 根据关联条件查询当前模型.
     *
     * @param string $relation 关联方法名
     * @param mixed  $operator 比较操作符
     * @param int    $count    个数
     * @param string $id       关联表的统计字段
     * @param string $joinType JOIN类型
     *
     * @return $this
     */
    public function has(string $relation, string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '')
    {
        return $this->model->has($relation, $operator, $count, $id, $joinType, $this);
    }
 
    /**
     * 根据关联条件查询当前模型.
     *
     * @param string $relation 关联方法名
     * @param mixed  $where    查询条件(数组或者闭包)
     * @param mixed  $fields   字段
     * @param string $joinType JOIN类型
     *
     * @return $this
     */
    public function hasWhere(string $relation, $where = [], string $fields = '*', string $joinType = '')
    {
        return $this->model->hasWhere($relation, $where, $fields, $joinType, $this);
    }
 
    /**
     * JSON字段数据转换.
     *
     * @param array $result 查询数据
     *
     * @return void
     */
    protected function jsonModelResult(array &$result): void
    {
        $withAttr = $this->options['with_attr'];
        foreach ($this->options['json'] as $name) {
            if (!isset($result[$name])) {
                continue;
            }
 
            $jsonData = json_decode($result[$name], true);
            if (json_last_error() !== JSON_ERROR_NONE) {
                continue;
            }
 
            if (isset($withAttr[$name])) {
                foreach ($withAttr[$name] as $key => $closure) {
                    $jsonData[$key] = $closure($jsonData[$key] ?? null, $jsonData);
                }
            }
 
            $result[$name] = !$this->options['json_assoc'] ? (object) $jsonData : $jsonData;
        }
    }
 
    /**
     * 查询数据转换为模型数据集对象
     *
     * @param array $resultSet 数据集
     *
     * @return ModelCollection
     */
    protected function resultSetToModelCollection(array $resultSet): ModelCollection
    {
        if (empty($resultSet)) {
            return $this->model->toCollection();
        }
 
        $this->options['is_resultSet'] = true;
 
        foreach ($resultSet as $key => &$result) {
            // 数据转换为模型对象
            $this->resultToModel($result);
        }
 
        foreach (['with', 'with_join'] as $with) {
            // 关联预载入
            if (!empty($this->options[$with])) {
                $result->eagerlyResultSet(
                    $resultSet,
                    $this->options[$with],
                    $this->options['with_relation_attr'],
                    'with_join' == $with,
                    $this->options['with_cache'] ?? false
                );
            }
        }
 
        // 模型数据集转换
        return $this->model->toCollection($resultSet);
    }
 
    /**
     * 查询数据转换为模型对象
     *
     * @param array $result 查询数据
     *
     * @return void
     */
    protected function resultToModel(array &$result): void
    {
        // JSON数据处理
        if (!empty($this->options['json'])) {
            $this->jsonModelResult($result);
        }
 
        // 实时读取延迟数据
        if (!empty($this->options['lazy_fields'])) {
            $id = $this->getKey($result);
            foreach ($this->options['lazy_fields'] as $field) {
                if (isset($result[$field])) {
                    $result[$field] += $this->getLazyFieldValue($field, $id);
                }
            }
        }
 
        $result = $this->model->newInstance(
            $result,
            !empty($this->options['is_resultSet']) ? null : $this->getModelUpdateCondition($this->options),
            $this->options
        );
 
        if ($this->suffix) {
            $result->setSuffix($this->suffix);
        }
            
        // 模型数据处理
        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,
                        $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->withFieldAttr($this->options['with_attr']);
        }
 
        if (!empty($this->options['mapping'])) {
            $result->mapping($this->options['mapping']);
        }
 
        foreach (['hidden', 'visible', 'append'] as $name) {
            if (!empty($this->options[$name])) {
                [$value, $merge] = $this->options[$name];
                $result->$name($value, $merge);
            }
        }
 
        // 刷新原始数据
        $result->refreshOrigin();
    }
}