chengkun
2025-09-15 17e42d4e0fa95c7af0173be4ef4768eeb6090d5f
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
<?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\model\concern;
 
use Closure;
use think\Collection;
use think\db\BaseQuery as Query;
use think\db\exception\DbException as Exception;
use think\db\exception\InvalidArgumentException;
use think\helper\Str;
use think\Model;
use think\model\Relation;
use think\model\relation\BelongsTo;
use think\model\relation\BelongsToMany;
use think\model\relation\HasMany;
use think\model\relation\HasManyThrough;
use think\model\relation\HasOne;
use think\model\relation\HasOneThrough;
use think\model\relation\MorphMany;
use think\model\relation\MorphOne;
use think\model\relation\MorphTo;
use think\model\relation\MorphToMany;
use think\model\relation\OneToOne;
 
/**
 * 模型关联处理.
 */
trait RelationShip
{
    /**
     * 父关联模型对象
     *
     * @var object
     */
    private $parent;
 
    /**
     * 模型关联数据.
     *
     * @var array
     */
    private $relation = [];
 
    /**
     * 预载入关联模型
     *
     * @var array
     */
    protected $with = [];
 
    /**
     * 关联写入定义信息.
     *
     * @var array
     */
    private $together = [];
 
    /**
     * 关联自动写入信息.
     *
     * @var array
     */
    protected $relationWrite = [];
 
    /**
     * 设置父关联对象
     *
     * @param Model $model 模型对象
     *
     * @return $this
     */
    public function setParent(Model $model)
    {
        $this->parent = $model;
 
        return $this;
    }
 
    /**
     * 获取父关联对象
     *
     * @return Model
     */
    public function getParent(): Model
    {
        return $this->parent;
    }
 
    /**
     * 获取当前模型的关联模型数据.
     *
     * @param string $name 关联方法名
     * @param bool   $auto 不存在是否自动获取
     *
     * @return mixed
     */
    public function getRelation(?string $name = null, bool $auto = false)
    {
        if (is_null($name)) {
            return $this->relation;
        }
 
        if (array_key_exists($name, $this->relation)) {
            return $this->relation[$name];
        } elseif ($auto) {
            $relation = Str::camel($name);
 
            return $this->getRelationValue($relation);
        }
    }
 
    /**
     * 设置关联数据对象值
     *
     * @param string $name  属性名
     * @param mixed  $value 属性值
     * @param array  $data  数据
     *
     * @return $this
     */
    public function setRelation(string $name, $value, array $data = [])
    {
        // 检测修改器
        $method = 'set' . Str::studly($name) . 'Attr';
 
        if (method_exists($this, $method)) {
            $value = $this->$method($value, array_merge($this->data, $data));
        }
 
        $name = $this->getRealFieldName($name);
 
        $this->relation[$name]  = $value;
        $this->with[$name]      = true;
 
        return $this;
    }
 
    /**
     * 查询当前模型的关联数据.
     *
     * @param array $relations        关联名
     * @param array $withRelationAttr 关联获取器
     *
     * @return void
     */
    public function relationQuery(array $relations, array $withRelationAttr = []): void
    {
        foreach ($relations as $key => $relation) {
            $subRelation = [];
            $closure = null;
 
            if ($relation instanceof Closure) {
                // 支持闭包查询过滤关联条件
                $closure    = $relation;
                $relation   = $key;
            }
 
            if (is_array($relation)) {
                $subRelation    = $relation;
                $relation       = $key;
            } elseif (str_contains($relation, '.')) {
                [$relation, $subRelation] = explode('.', $relation, 2);
            }
 
            $method         = Str::camel($relation);
            $relationName   = Str::snake($relation);
            $relationResult = $this->$method();
 
            if (isset($withRelationAttr[$relationName])) {
                $relationResult->withAttr($withRelationAttr[$relationName]);
            }
 
            $this->setRelation($relation, $relationResult->getRelation((array) $subRelation, $closure));
        }
    }
 
    /**
     * 关联数据写入.
     *
     * @param array $relation 关联
     *
     * @return $this
     */
    public function together(array $relation)
    {
        $this->together = $relation;
 
        $this->checkAutoRelationWrite();
 
        return $this;
    }
 
    /**
     * 根据关联条件查询当前模型.
     *
     * @param string $relation 关联方法名
     * @param mixed  $operator 比较操作符
     * @param int    $count    个数
     * @param string $id       关联表的统计字段
     * @param string $joinType JOIN类型
     * @param Query  $query    Query对象
     *
     * @return Query
     */
    public static function has(string $relation, string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '', ?Query $query = null): Query
    {
        return (new static())
            ->$relation()
            ->has($operator, $count, $id, $joinType, $query);
    }
 
    /**
     * 根据关联条件查询当前模型.
     *
     * @param string $relation 关联方法名
     * @param mixed  $where    查询条件(数组或者闭包)
     * @param mixed  $fields   字段
     * @param string $joinType JOIN类型
     * @param Query  $query    Query对象
     *
     * @return Query
     */
    public static function hasWhere(string $relation, $where = [], string $fields = '*', string $joinType = '', ?Query $query = null): Query
    {
        return (new static())
            ->$relation()
            ->hasWhere($where, $fields, $joinType, $query);
    }
 
    /**
     * 预载入关联查询 JOIN方式.
     *
     * @param Query   $query    Query对象
     * @param string  $relation 关联方法名
     * @param mixed   $field    字段
     * @param string  $joinType JOIN类型
     * @param Closure $closure  闭包
     * @param bool    $first
     *
     * @return bool
     */
    public function eagerly(Query $query, string $relation, $field, string $joinType = '', ?Closure $closure = null, bool $first = false): bool
    {
        $relation   = Str::camel($relation);
        $class      = $this->$relation();
 
        if ($class instanceof OneToOne) {
            $class->eagerly($query, $relation, $field, $joinType, $closure, $first);
 
            return true;
        }
 
        return false;
    }
 
    /**
     * 预载入关联查询 返回数据集.
     *
     * @param array  $resultSet        数据集
     * @param array  $relations        关联名
     * @param array  $withRelationAttr 关联获取器
     * @param bool   $join             是否为JOIN方式
     * @param mixed  $cache            关联缓存
     *
     * @return void
     */
    public function eagerlyResultSet(array &$resultSet, array $relations, array $withRelationAttr = [], bool $join = false, $cache = false): void
    {
        foreach ($relations as $key => $relation) {
            $subRelation    = [];
            $closure        = null;
 
            if ($relation instanceof Closure) {
                $closure    = $relation;
                $relation   = $key;
            }
 
            if (is_array($relation)) {
                $subRelation    = $relation;
                $relation       = $key;
            } elseif (str_contains($relation, '.')) {
                [$relation, $subRelation] = explode('.', $relation, 2);
 
                $subRelation = [$subRelation];
            }
 
            $relationName   = $relation;
            $relation       = Str::camel($relation);
            $relationResult = $this->$relation();
 
            if (isset($withRelationAttr[$relationName])) {
                $relationResult->withAttr($withRelationAttr[$relationName]);
            }
 
            if (is_scalar($cache)) {
                $relationCache = [$cache];
            } else {
                $relationCache = $cache[$relationName] ?? $cache;
            }
 
            $relationResult->eagerlyResultSet($resultSet, $relationName, $subRelation, $closure, $relationCache, $join);
 
            $this->with[$relationName] = true;
        }
    }
 
    /**
     * 预载入关联查询 返回模型对象
     *
     * @param array $relations        关联
     * @param array $withRelationAttr 关联获取器
     * @param bool  $join             是否为JOIN方式
     * @param mixed $cache            关联缓存
     *
     * @return void
     */
    public function eagerlyResult(array $relations, array $withRelationAttr = [], bool $join = false, $cache = false): void
    {
        foreach ($relations as $key => $relation) {
            $subRelation    = [];
            $closure        = null;
 
            if ($relation instanceof Closure) {
                $closure    = $relation;
                $relation   = $key;
            }
 
            if (is_array($relation)) {
                $subRelation    = $relation;
                $relation       = $key;
            } elseif (str_contains($relation, '.')) {
                [$relation, $subRelation] = explode('.', $relation, 2);
 
                $subRelation = [$subRelation];
            }
 
            $relationName   = $relation;
            $relation       = Str::camel($relation);
            $relationResult = $this->$relation();
 
            if (isset($withRelationAttr[$relationName])) {
                $relationResult->withAttr($withRelationAttr[$relationName]);
            }
 
            if (is_scalar($cache)) {
                $relationCache = [$cache];
            } else {
                $relationCache = $cache[$relationName] ?? [];
            }
 
            $relationResult->eagerlyResult($this, $relationName, $subRelation, $closure, $relationCache, $join);
        }
    }
 
    /**
     * 绑定(一对一)关联属性到当前模型.
     *
     * @param string $relation 关联名称
     * @param array  $attrs    绑定属性
     *
     * @throws Exception
     *
     * @return $this
     */
    public function bindAttr(string $relation, array $attrs = [])
    {
        $relation = $this->getRelation($relation, true);
 
        foreach ($attrs as $key => $attr) {
            if (is_numeric($key)) {
                if (!is_string($attr)) {
                    throw new InvalidArgumentException('bind attr must be string:' . $key);
                }
 
                $key = $attr;
            }
 
            if (null !== $this->getOrigin($key)) {
                throw new Exception('bind attr has exists:' . $key);
            }
 
            if ($attr instanceof Closure) {
                $value = $attr($relation, $key, $this);
            } else {
                $value = $relation?->getAttr($attr);
            }
 
            $this->set($key, $value);
        }
 
        return $this;
    }
 
    /**
     * 关联统计
     *
     * @param Query  $query       查询对象
     * @param array  $relations   关联名
     * @param string $aggregate   聚合查询方法
     * @param string $field       字段
     * @param bool   $useSubQuery 子查询
     *
     * @return void
     */
    public function relationCount(Query $query, array $relations, string $aggregate = 'sum', string $field = '*', bool $useSubQuery = true): void
    {
        foreach ($relations as $key => $relation) {
            $closure = $name = null;
 
            if ($relation instanceof Closure) {
                $closure    = $relation;
                $relation   = $key;
            } elseif (is_string($key)) {
                $name       = $relation;
                $relation   = $key;
            }
 
            $relation = Str::camel($relation);
 
            if ($useSubQuery) {
                $count = $this->$relation()->getRelationCountQuery($closure, $aggregate, $field, $name);
            } else {
                $count = $this->$relation()->relationCount($this, $closure, $aggregate, $field, $name);
            }
 
            if (empty($name)) {
                $name = Str::snake($relation) . '_' . $aggregate;
            }
 
            if ($useSubQuery) {
                $query->field(['(' . $count . ')' => $name]);
            } else {
                $this->setAttr($name, $count);
            }
        }
    }
 
    /**
     * HAS ONE 关联定义.
     *
     * @param string $model      模型名
     * @param string $foreignKey 关联外键
     * @param string $localKey   当前主键
     *
     * @return HasOne
     */
    public function hasOne(string $model, string $foreignKey = '', string $localKey = ''): HasOne
    {
        // 记录当前关联信息
        $model      = $this->parseModel($model);
        $localKey   = $localKey ?: $this->getPk();
        $foreignKey = $foreignKey ?: $this->getForeignKey($this->name);
 
        return new HasOne($this, $model, $foreignKey, $localKey);
    }
 
    /**
     * BELONGS TO 关联定义.
     *
     * @param string $model      模型名
     * @param string $foreignKey 关联外键
     * @param string $localKey   关联主键
     *
     * @return BelongsTo
     */
    public function belongsTo(string $model, string $foreignKey = '', string $localKey = ''): BelongsTo
    {
        // 记录当前关联信息
        $model      = $this->parseModel($model);
        $foreignKey = $foreignKey ?: $this->getForeignKey((new $model())->getName());
        $localKey   = $localKey ?: (new $model())->getPk();
        $trace      = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
        $relation   = Str::snake($trace[1]['function']);
 
        return new BelongsTo($this, $model, $foreignKey, $localKey, $relation);
    }
 
    /**
     * HAS MANY 关联定义.
     *
     * @param string $model      模型名
     * @param string $foreignKey 关联外键
     * @param string $localKey   当前主键
     *
     * @return HasMany
     */
    public function hasMany(string $model, string $foreignKey = '', string $localKey = ''): HasMany
    {
        // 记录当前关联信息
        $model      = $this->parseModel($model);
        $localKey   = $localKey ?: $this->getPk();
        $foreignKey = $foreignKey ?: $this->getForeignKey($this->name);
 
        return new HasMany($this, $model, $foreignKey, $localKey);
    }
 
    /**
     * HAS MANY 远程关联定义.
     *
     * @param string $model      模型名
     * @param string $through    中间模型名
     * @param string $foreignKey 关联外键
     * @param string $throughKey 关联外键
     * @param string $localKey   当前主键
     * @param string $throughPk  中间表主键
     *
     * @return HasManyThrough
     */
    public function hasManyThrough(string $model, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = ''): HasManyThrough
    {
        // 记录当前关联信息
        $model      = $this->parseModel($model);
        $through    = $this->parseModel($through);
        $localKey   = $localKey ?: $this->getPk();
        $foreignKey = $foreignKey ?: $this->getForeignKey($this->name);
        $throughKey = $throughKey ?: $this->getForeignKey((new $through())->getName());
        $throughPk  = $throughPk ?: (new $through())->getPk();
 
        return new HasManyThrough($this, $model, $through, $foreignKey, $throughKey, $localKey, $throughPk);
    }
 
    /**
     * HAS ONE 远程关联定义.
     *
     * @param string $model      模型名
     * @param string $through    中间模型名
     * @param string $foreignKey 关联外键
     * @param string $throughKey 关联外键
     * @param string $localKey   当前主键
     * @param string $throughPk  中间表主键
     *
     * @return HasOneThrough
     */
    public function hasOneThrough(string $model, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = ''): HasOneThrough
    {
        // 记录当前关联信息
        $model      = $this->parseModel($model);
        $through    = $this->parseModel($through);
        $localKey   = $localKey ?: $this->getPk();
        $foreignKey = $foreignKey ?: $this->getForeignKey($this->name);
        $throughKey = $throughKey ?: $this->getForeignKey((new $through())->getName());
        $throughPk  = $throughPk ?: (new $through())->getPk();
 
        return new HasOneThrough($this, $model, $through, $foreignKey, $throughKey, $localKey, $throughPk);
    }
 
    /**
     * BELONGS TO MANY 关联定义.
     *
     * @param string $model      模型名
     * @param string $middle     中间表/模型名
     * @param string $foreignKey 关联外键
     * @param string $localKey   当前模型关联键
     *
     * @return BelongsToMany
     */
    public function belongsToMany(string $model, string $middle = '', string $foreignKey = '', string $localKey = ''): BelongsToMany
    {
        // 记录当前关联信息
        $model      = $this->parseModel($model);
        $name       = Str::snake(class_basename($model));
        $middle     = $middle ?: Str::snake($this->name) . '_' . $name;
        $foreignKey = $foreignKey ?: $name . '_id';
        $localKey   = $localKey ?: $this->getForeignKey($this->name);
 
        return new BelongsToMany($this, $model, $middle, $foreignKey, $localKey);
    }
 
    /**
     * MORPH  One 关联定义.
     *
     * @param string       $model 模型名
     * @param string|array $morph 多态字段信息
     * @param string       $type  多态类型
     *
     * @return MorphOne
     */
    public function morphOne(string $model, string|array|null $morph = null, string $type = ''): MorphOne
    {
        // 记录当前关联信息
        $model = $this->parseModel($model);
 
        if (is_null($morph)) {
            $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
            $morph = Str::snake($trace[1]['function']);
        }
 
        [$morphType, $foreignKey] = $this->parseMorph($morph);
 
        $type = $type ?: get_class($this);
 
        return new MorphOne($this, $model, $foreignKey, $morphType, $type);
    }
 
    /**
     * MORPH  MANY 关联定义.
     *
     * @param string       $model 模型名
     * @param string|array $morph 多态字段信息
     * @param string       $type  多态类型
     *
     * @return MorphMany
     */
    public function morphMany(string $model, string|array|null $morph = null, string $type = ''): MorphMany
    {
        // 记录当前关联信息
        $model = $this->parseModel($model);
 
        if (is_null($morph)) {
            $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
            $morph = Str::snake($trace[1]['function']);
        }
 
        $type = $type ?: get_class($this);
 
        [$morphType, $foreignKey] = $this->parseMorph($morph);
 
        return new MorphMany($this, $model, $foreignKey, $morphType, $type);
    }
 
    /**
     * MORPH TO 关联定义.
     *
     * @param string|array $morph 多态字段信息
     * @param array        $alias 多态别名定义
     *
     * @return MorphTo
     */
    public function morphTo(string|array|null $morph = null, array $alias = []): MorphTo
    {
        $trace      = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
        $relation   = Str::snake($trace[1]['function']);
 
        if (is_null($morph)) {
            $morph  = $relation;
        }
 
        [$morphType, $foreignKey] = $this->parseMorph($morph);
 
        return new MorphTo($this, $morphType, $foreignKey, $alias, $relation);
    }
 
    /**
     * MORPH TO MANY关联定义.
     *
     * @param string       $model    模型名
     * @param string       $middle   中间表名/模型名
     * @param string|array $morph    多态字段信息
     * @param string       $localKey 当前模型关联键
     *
     * @return MorphToMany
     */
    public function morphToMany(string $model, string $middle, string|array|null $morph = null, ?string $localKey = null): MorphToMany
    {
        if (is_null($morph)) {
            $morph = $middle;
        }
 
        [$morphType, $morphKey] = $this->parseMorph($morph);
 
        $model      = $this->parseModel($model);
        $name       = Str::snake(class_basename($model));
        $localKey   = $localKey ?: $this->getForeignKey($name);
 
        return new MorphToMany($this, $model, $middle, $morphType, $morphKey, $localKey);
    }
 
    /**
     * MORPH BY MANY关联定义.
     *
     * @param string       $model      模型名
     * @param string       $middle     中间表名/模型名
     * @param string|array $morph      多态字段信息
     * @param string       $foreignKey 关联外键
     *
     * @return MorphToMany
     */
    public function morphByMany(string $model, string $middle, string|array|null $morph = null, ?string $foreignKey = null): MorphToMany
    {
        if (is_null($morph)) {
            $morph = $middle;
        }
 
        [$morphType, $morphKey] = $this->parseMorph($morph);
 
        $model      = $this->parseModel($model);
        $foreignKey = $foreignKey ?: $this->getForeignKey($this->name);
 
        return new MorphToMany($this, $model, $middle, $morphType, $morphKey, $foreignKey, true);
    }
 
    /**
     * 解析多态
     *
     * @param string|array $morph
     *
     * @return array
     */
    protected function parseMorph(string|array $morph): array
    {
        if (is_array($morph)) {
            [$morphType, $foreignKey] = $morph;
        } else {
            $morphType  = $morph . '_type';
            $foreignKey = $morph . '_id';
        }
 
        return [$morphType, $foreignKey];
    }
 
    /**
     * 解析模型的完整命名空间.
     *
     * @param string $model 模型名(或者完整类名)
     *
     * @return string
     */
    protected function parseModel(string $model): string
    {
        if (!str_contains($model, '\\')) {
            $path = explode('\\', static::class);
            array_pop($path);
            array_push($path, Str::studly($model));
            $model = implode('\\', $path);
        }
 
        return $model;
    }
 
    /**
     * 获取模型的默认外键名.
     *
     * @param string $name 模型名
     *
     * @return string
     */
    protected function getForeignKey(string $name): string
    {
        if (str_contains($name, '\\')) {
            $name = class_basename($name);
        }
 
        return Str::snake($name) . '_id';
    }
 
    /**
     * 检查属性是否为关联属性 如果是则返回关联方法名.
     *
     * @param string $attr 关联属性名
     *
     * @return string|false
     */
    protected function isRelationAttr(string $attr)
    {
        $relation = Str::camel($attr);
 
        if ((method_exists($this, $relation) && !method_exists('think\Model', $relation)) || isset(static::$macro[static::class][$relation])) {
            return $relation;
        }
 
        return false;
    }
 
    /**
     * 智能获取关联模型数据.
     *
     * @param Relation $modelRelation 模型关联对象
     *
     * @return mixed
     */
    protected function getRelationData(Relation $modelRelation)
    {
        if (
            $this->parent && !$modelRelation->isSelfRelation()
            && get_class($this->parent) == get_class($modelRelation->getModel())
            && ($modelRelation instanceof OneToOne || $modelRelation instanceof HasOneThrough || $modelRelation instanceof MorphTo || $modelRelation instanceof MorphOne)
        ) {
            if(empty($this->parent->parent)) $this->parent->parent = $this;
            return $this->parent;
        }
 
        // 获取关联数据
        return $modelRelation->getRelation();
    }
 
    /**
     * 关联数据自动写入检查.
     *
     * @return void
     */
    protected function checkAutoRelationWrite(): void
    {
        foreach ($this->together as $key => $name) {
            if (is_array($name)) {
                if (key($name) === 0) {
                    $this->relationWrite[$key] = [];
                    // 绑定关联属性
                    foreach ($name as $val) {
                        if (isset($this->data[$val])) {
                            $this->relationWrite[$key][$val] = $this->data[$val];
                        }
                    }
                } else {
                    // 直接传入关联数据
                    $this->relationWrite[$key] = $name;
                }
            } elseif (isset($this->relation[$name])) {
                $this->relationWrite[$name] = $this->relation[$name];
            } elseif (isset($this->data[$name])) {
                $this->relationWrite[$name] = $this->data[$name];
                unset($this->data[$name]);
            }
        }
    }
 
    /**
     * 自动关联数据更新(针对一对一关联).
     *
     * @return void
     */
    protected function autoRelationUpdate(): void
    {
        foreach ($this->relationWrite as $name => $val) {
            if ($val instanceof Model) {
                $val->exists(true)->save();
            } else {
                $model = $this->getRelation($name, true);
 
                if ($model instanceof Model) {
                    $model->exists(true)->save($val);
                }
            }
        }
    }
 
    /**
     * 自动关联数据写入(针对一对一关联).
     *
     * @return void
     */
    protected function autoRelationInsert(): void
    {
        foreach ($this->relationWrite as $name => $val) {
            $method = Str::camel($name);
            $this->$method()->save($val);
        }
    }
 
    /**
     * 自动关联数据删除(支持一对一及一对多关联).
     *
     * @param bool $force 强制删除
     *
     * @return void
     */
    protected function autoRelationDelete($force = false): void
    {
        foreach ($this->relationWrite as $key => $name) {
            $name   = is_numeric($key) ? $name : $key;
            $result = $this->getRelation($name, true);
 
            if ($result instanceof Model) {
                $result->force($force)->delete();
            } elseif ($result instanceof Collection) {
                foreach ($result as $model) {
                    $model->force($force)->delete();
                }
            }
        }
    }
 
    /**
     * 移除当前模型的关联属性.
     *
     * @return $this
     */
    public function removeRelation()
    {
        $this->relation = [];
 
        return $this;
    }
}