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
<?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>
// +----------------------------------------------------------------------
 
namespace think\model\relation;
 
use Closure;
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;
 
/**
 * 一对一关联基础类.
 */
abstract class OneToOne extends Relation
{
    /**
     * JOIN类型.
     *
     * @var string
     */
    protected $joinType = 'INNER';
 
    /**
     * 绑定的关联属性.
     *
     * @var array
     */
    protected $bindAttr = [];
 
    /**
     * 关联名.
     *
     * @var string
     */
    protected $relation;
 
    /**
     * 设置join类型.
     *
     * @param string $type JOIN类型
     *
     * @return $this
     */
    public function joinType(string $type)
    {
        $this->joinType = $type;
 
        return $this;
    }
 
    /**
     * 预载入关联查询(JOIN方式).
     *
     * @param Query   $query    查询对象
     * @param string  $relation 关联名
     * @param mixed   $field    关联字段
     * @param string  $joinType JOIN方式
     * @param Closure $closure  闭包条件
     * @param bool    $first
     *
     * @return void
     */
    public function eagerly(Query $query, string $relation, $field = true, string $joinType = '', ?Closure $closure = null, bool $first = false): void
    {
        $name = Str::snake(class_basename($this->parent));
 
        if ($first) {
            $table = $query->getTable();
            $query->table([$table => $name]);
 
            if ($query->getOptions('field')) {
                $masterField = $query->getOptions('field');
                $query->removeOption('field');
            } else {
                $masterField = true;
            }
 
            $query->tableField($masterField, $table, $name);
        }
 
        // 预载入封装
        $joinTable  = $this->query->getTable();
        $joinAlias  = Str::snake($relation);
        $joinType   = $joinType ?: $this->joinType;
 
        $query->via($joinAlias);
 
        if ($this instanceof BelongsTo) {
            $foreignKeyExp = $this->foreignKey;
 
            if (!str_contains($foreignKeyExp, '.')) {
                $foreignKeyExp = $name . '.' . $this->foreignKey;
            }
 
            $joinOn = $foreignKeyExp . '=' . $joinAlias . '.' . $this->localKey;
        } else {
            $foreignKeyExp = $this->foreignKey;
 
            if (!str_contains($foreignKeyExp, '.')) {
                $foreignKeyExp = $joinAlias . '.' . $this->foreignKey;
            }
 
            $joinOn = $name . '.' . $this->localKey . '=' . $foreignKeyExp;
        }
 
        if ($closure) {
            // 执行闭包查询
            $closure($query);
 
            // 使用withField指定获取关联的字段
            $withField = $this->query->getOptions('field');
            if ($withField) {
                $field = $withField;
            }
        }
 
        $query->join([$joinTable => $joinAlias], $joinOn, $joinType)
            ->tableField($field, $joinTable, $joinAlias, $joinAlias . '__');
    }
 
    /**
     *  预载入关联查询(数据集).
     *
     * @param array   $resultSet
     * @param string  $relation
     * @param array   $subRelation
     * @param Closure $closure
     *
     * @return mixed
     */
    abstract protected function eagerlySet(array &$resultSet, string $relation, array $subRelation = [], ?Closure $closure = null);
 
    /**
     * 预载入关联查询(数据).
     *
     * @param Model   $result
     * @param string  $relation
     * @param array   $subRelation
     * @param Closure $closure
     *
     * @return mixed
     */
    abstract protected function eagerlyOne(Model $result, string $relation, array $subRelation = [], ?Closure $closure = null);
 
    /**
     * 预载入关联查询(数据集).
     *
     * @param array   $resultSet   数据集
     * @param string  $relation    当前关联名
     * @param array   $subRelation 子关联名
     * @param Closure $closure     闭包
     * @param array   $cache       关联缓存
     * @param bool    $join        是否为JOIN方式
     *
     * @return void
     */
    public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation = [], ?Closure $closure = null, array $cache = [], bool $join = false): void
    {
        if ($join) {
            // 模型JOIN关联组装
            foreach ($resultSet as $result) {
                $this->match($this->model, $relation, $result);
            }
        } else {
            // IN查询
            $this->eagerlySet($resultSet, $relation, $subRelation, $closure, $cache);
        }
    }
 
    /**
     * 预载入关联查询(数据).
     *
     * @param Model   $result      数据对象
     * @param string  $relation    当前关联名
     * @param array   $subRelation 子关联名
     * @param Closure $closure     闭包
     * @param array   $cache       关联缓存
     * @param bool    $join        是否为JOIN方式
     *
     * @return void
     */
    public function eagerlyResult(Model $result, string $relation, array $subRelation = [], ?Closure $closure = null, array $cache = [], bool $join = false): void
    {
        if ($join) {
            // 模型JOIN关联组装
            $this->match($this->model, $relation, $result);
        } else {
            // IN查询
            $this->eagerlyOne($result, $relation, $subRelation, $closure, $cache);
        }
    }
 
    /**
     * 保存(新增)当前关联数据对象
     *
     * @param array|Model $data    数据 可以使用数组 关联模型对象
     * @param bool  $replace 是否自动识别更新和写入
     *
     * @return Model|false
     */
    public function save(array|Model $data, bool $replace = true)
    {
        $model = $this->make();
 
        return $model->replace($replace)->save($data) ? $model : false;
    }
 
    /**
     * 创建关联对象实例.
     *
     * @param array|Model $data
     *
     * @return Model
     */
    public function make(array|Model $data = []): Model
    {
        if ($data instanceof Model) {
            $data = $data->getData();
        }
 
        // 保存关联表数据
        $data[$this->foreignKey] = $this->parent->{$this->localKey};
 
        return (new $this->model($data))->setSuffix($this->getModel()->getSuffix());
    }
 
    /**
     * 绑定关联表的属性到父模型属性.
     *
     * @param array $attr 要绑定的属性列表
     *
     * @return $this
     */
    public function bind(array $attr)
    {
        $this->bindAttr = $attr;
 
        return $this;
    }
 
    /**
     * 获取绑定属性.
     *
     * @return array
     */
    public function getBindAttr(): array
    {
        return $this->bindAttr;
    }
 
    /**
     * 一对一 关联模型预查询拼装.
     *
     * @param string $model    模型名称
     * @param string $relation 关联名
     * @param Model  $result   模型对象实例
     *
     * @return void
     */
    protected function match(string $model, string $relation, Model $result): void
    {
        // 重新组装模型数据
        foreach ($result->getData() as $key => $val) {
            if (str_contains($key, '__')) {
                [$name, $attr] = explode('__', $key, 2);
                if ($name == Str::snake($relation)) {
                    $list[$relation][$attr] = $val;
                    unset($result->$key);
                }
            }
        }
 
        if (isset($list[$relation])) {
            $array = array_unique($list[$relation]);
 
            if (count($array) == 1 && null === current($array)) {
                $relationModel = null;
            } else {
                $relationModel = new $model($list[$relation]);
                $relationModel->setParent(clone $result);
                $relationModel->exists(true);
            }
 
            if (!empty($this->bindAttr)) {
                $this->bindAttr($result, $relationModel);
            }
        } else {
            $relationModel = null;
        }
 
        $result->setRelation($relation, $relationModel);
    }
 
    /**
     * 绑定关联属性到父模型.
     *
     * @param Model $result 父模型对象
     * @param Model $model  关联模型对象
     *
     * @throws Exception
     *
     * @return void
     */
    protected function bindAttr(Model $result, ?Model $model = null): void
    {
        foreach ($this->bindAttr as $key => $attr) {
            if (is_numeric($key)) {
                if (!is_string($attr)) {
                    throw new InvalidArgumentException('bind attr must be string:' . $key);
                }
 
                $key = $attr;
            }
 
            if (null !== $result->getOrigin($key)) {
                throw new Exception('bind attr has exists:' . $key);
            }
 
            if ($attr instanceof Closure) {
                $value = $attr($model, $key, $result);
            } else {
                $value = $model?->getAttr($attr);
            }
 
            $result->setAttr($key, $value);
        }
    }
 
    /**
     * 一对一 关联模型预查询(IN方式).
     *
     * @param array   $where       关联预查询条件
     * @param string  $key         关联键名
     * @param array   $subRelation 子关联
     * @param Closure $closure
     * @param array   $cache       关联缓存
     *
     * @return array
     */
    protected function eagerlyWhere(array $where, string $key, array $subRelation = [], ?Closure $closure = null, array $cache = [])
    {
        // 预载入关联查询 支持嵌套预载入
        if ($closure) {
            $this->baseQuery = true;
            $closure($this->query);
        }
 
        $list = $this->query
            ->where($where)
            ->with($subRelation)
            ->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
            ->select();
 
        // 组装模型数据
        $data = [];
 
        foreach ($list as $set) {
            if (!isset($data[$set->$key])) {
                $data[$set->$key] = $set;
            }
        }
 
        return $data;
    }
}