|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: liu21st <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
|
|
12
|
|
|
namespace think\model\relation; |
|
13
|
|
|
|
|
14
|
|
|
use think\Collection; |
|
15
|
|
|
use think\db\Query; |
|
16
|
|
|
use think\Exception; |
|
17
|
|
|
use think\Loader; |
|
18
|
|
|
use think\Model; |
|
19
|
|
|
use think\model\Pivot; |
|
20
|
|
|
use think\model\Relation; |
|
21
|
|
|
|
|
22
|
|
|
class BelongsToMany extends Relation |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
// 中间表表名 |
|
25
|
|
|
protected $middle; |
|
26
|
|
|
// 中间表模型名称 |
|
27
|
|
|
protected $pivotName; |
|
28
|
|
|
// 中间表数据名称 |
|
29
|
|
|
protected $pivotDataName = 'pivot'; |
|
30
|
|
|
// 中间表模型对象 |
|
31
|
|
|
protected $pivot; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* 架构函数 |
|
35
|
|
|
* @access public |
|
36
|
|
|
* @param Model $parent 上级模型对象 |
|
37
|
|
|
* @param string $model 模型名 |
|
38
|
|
|
* @param string $table 中间表名 |
|
39
|
|
|
* @param string $foreignKey 关联模型外键 |
|
40
|
|
|
* @param string $localKey 当前模型关联键 |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(Model $parent, $model, $table, $foreignKey, $localKey) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->parent = $parent; |
|
45
|
|
|
$this->model = $model; |
|
|
|
|
|
|
46
|
|
|
$this->foreignKey = $foreignKey; |
|
47
|
|
|
$this->localKey = $localKey; |
|
48
|
|
|
|
|
49
|
|
|
if (false !== strpos($table, '\\')) { |
|
50
|
|
|
$this->pivotName = $table; |
|
51
|
|
|
$this->middle = basename(str_replace('\\', '/', $table)); |
|
52
|
|
|
} else { |
|
53
|
|
|
$this->middle = $table; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->query = (new $model)->db(); |
|
57
|
|
|
$this->pivot = $this->newPivot(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
|
|
|
|
|
61
|
|
|
* 设置中间表模型 |
|
62
|
|
|
* @access public |
|
63
|
|
|
* @param $pivot |
|
|
|
|
|
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
|
|
public function pivot($pivot) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->pivotName = $pivot; |
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* 设置中间表数据名称 |
|
74
|
|
|
* @access public |
|
75
|
|
|
* @param string $name |
|
|
|
|
|
|
76
|
|
|
* @return $this |
|
77
|
|
|
*/ |
|
78
|
|
|
public function pivotDataName($name) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->pivotDataName = $name; |
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
|
|
|
|
|
85
|
|
|
* 获取中间表更新条件 |
|
86
|
|
|
* @param $data |
|
|
|
|
|
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getUpdateWhere($data) |
|
90
|
|
|
{ |
|
91
|
|
|
return [ |
|
92
|
|
|
$this->localKey => $data[$this->localKey], |
|
93
|
|
|
$this->foreignKey => $data[$this->foreignKey], |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* 实例化中间表模型 |
|
99
|
|
|
* @access public |
|
100
|
|
|
* @param array $data |
|
|
|
|
|
|
101
|
|
|
* @param bool $isUpdate |
|
|
|
|
|
|
102
|
|
|
* @return Pivot |
|
103
|
|
|
* @throws Exception |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function newPivot($data = [], $isUpdate = false) |
|
106
|
|
|
{ |
|
107
|
|
|
$class = $this->pivotName ?: '\\think\\model\\Pivot'; |
|
108
|
|
|
$pivot = new $class($data, $this->parent, $this->middle); |
|
109
|
|
|
|
|
110
|
|
|
if ($pivot instanceof Pivot) { |
|
111
|
|
|
return $isUpdate ? $pivot->isUpdate(true, $this->getUpdateWhere($data)) : $pivot; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
throw new Exception('pivot model must extends: \think\model\Pivot'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* 合成中间表模型 |
|
119
|
|
|
* @access protected |
|
120
|
|
|
* @param array|Collection|Paginator $models |
|
|
|
|
|
|
121
|
|
|
*/ |
|
|
|
|
|
|
122
|
|
|
protected function hydratePivot($models) |
|
123
|
|
|
{ |
|
124
|
|
|
foreach ($models as $model) { |
|
125
|
|
|
$pivot = []; |
|
126
|
|
|
|
|
127
|
|
|
foreach ($model->getData() as $key => $val) { |
|
128
|
|
|
if (strpos($key, '__')) { |
|
129
|
|
|
list($name, $attr) = explode('__', $key, 2); |
|
130
|
|
|
|
|
131
|
|
|
if ('pivot' == $name) { |
|
132
|
|
|
$pivot[$attr] = $val; |
|
133
|
|
|
unset($model->$key); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$model->setRelation($this->pivotDataName, $this->newPivot($pivot, true)); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* 创建关联查询Query对象 |
|
144
|
|
|
* @access protected |
|
145
|
|
|
* @return Query |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function buildQuery() |
|
148
|
|
|
{ |
|
149
|
|
|
$foreignKey = $this->foreignKey; |
|
150
|
|
|
$localKey = $this->localKey; |
|
151
|
|
|
|
|
152
|
|
|
// 关联查询 |
|
153
|
|
|
$pk = $this->parent->getPk(); |
|
154
|
|
|
|
|
155
|
|
|
$condition[] = ['pivot.' . $localKey, '=', $this->parent->$pk]; |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
return $this->belongsToManyQuery($foreignKey, $localKey, $condition); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* 延迟获取关联数据 |
|
162
|
|
|
* @access public |
|
163
|
|
|
* @param string $subRelation 子关联名 |
|
164
|
|
|
* @param \Closure $closure 闭包查询条件 |
|
165
|
|
|
* @return Collection |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getRelation($subRelation = '', $closure = null) |
|
168
|
|
|
{ |
|
169
|
|
|
if ($closure) { |
|
170
|
|
|
$closure($this->query); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$result = $this->buildQuery()->relation($subRelation)->select(); |
|
174
|
|
|
$this->hydratePivot($result); |
|
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
return $result; |
|
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* 重载select方法 |
|
181
|
|
|
* @access public |
|
182
|
|
|
* @param mixed $data |
|
|
|
|
|
|
183
|
|
|
* @return Collection |
|
184
|
|
|
*/ |
|
185
|
|
|
public function select($data = null) |
|
186
|
|
|
{ |
|
187
|
|
|
$result = $this->buildQuery()->select($data); |
|
188
|
|
|
$this->hydratePivot($result); |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
return $result; |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* 重载paginate方法 |
|
195
|
|
|
* @access public |
|
196
|
|
|
* @param null $listRows |
|
|
|
|
|
|
197
|
|
|
* @param bool $simple |
|
|
|
|
|
|
198
|
|
|
* @param array $config |
|
|
|
|
|
|
199
|
|
|
* @return Paginator |
|
200
|
|
|
*/ |
|
201
|
|
|
public function paginate($listRows = null, $simple = false, $config = []) |
|
202
|
|
|
{ |
|
203
|
|
|
$result = $this->buildQuery()->paginate($listRows, $simple, $config); |
|
204
|
|
|
$this->hydratePivot($result); |
|
205
|
|
|
|
|
206
|
|
|
return $result; |
|
|
|
|
|
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* 重载find方法 |
|
211
|
|
|
* @access public |
|
212
|
|
|
* @param mixed $data |
|
|
|
|
|
|
213
|
|
|
* @return Model |
|
214
|
|
|
*/ |
|
215
|
|
|
public function find($data = null) |
|
216
|
|
|
{ |
|
217
|
|
|
$result = $this->buildQuery()->find($data); |
|
218
|
|
|
if ($result) { |
|
219
|
|
|
$this->hydratePivot([$result]); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return $result; |
|
|
|
|
|
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* 查找多条记录 如果不存在则抛出异常 |
|
227
|
|
|
* @access public |
|
228
|
|
|
* @param array|string|Query|\Closure $data |
|
|
|
|
|
|
229
|
|
|
* @return Collection |
|
230
|
|
|
*/ |
|
231
|
|
|
public function selectOrFail($data = null) |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->failException(true)->select($data); |
|
|
|
|
|
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* 查找单条记录 如果不存在则抛出异常 |
|
238
|
|
|
* @access public |
|
239
|
|
|
* @param array|string|Query|\Closure $data |
|
|
|
|
|
|
240
|
|
|
* @return Model |
|
241
|
|
|
*/ |
|
242
|
|
|
public function findOrFail($data = null) |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->failException(true)->find($data); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* 根据关联条件查询当前模型 |
|
249
|
|
|
* @access public |
|
250
|
|
|
* @param string $operator 比较操作符 |
|
251
|
|
|
* @param integer $count 个数 |
|
252
|
|
|
* @param string $id 关联表的统计字段 |
|
253
|
|
|
* @param string $joinType JOIN类型 |
|
254
|
|
|
* @return Query |
|
255
|
|
|
*/ |
|
256
|
|
|
public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER') |
|
257
|
|
|
{ |
|
258
|
|
|
return $this->parent; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* 根据关联条件查询当前模型 |
|
263
|
|
|
* @access public |
|
264
|
|
|
* @param mixed $where 查询条件(数组或者闭包) |
|
|
|
|
|
|
265
|
|
|
* @param mixed $fields 字段 |
|
266
|
|
|
* @return Query |
|
267
|
|
|
* @throws Exception |
|
268
|
|
|
*/ |
|
269
|
|
|
public function hasWhere($where = [], $fields = null) |
|
270
|
|
|
{ |
|
271
|
|
|
throw new Exception('relation not support: hasWhere'); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* 设置中间表的查询条件 |
|
276
|
|
|
* @access public |
|
277
|
|
|
* @param string $field |
|
|
|
|
|
|
278
|
|
|
* @param string $op |
|
|
|
|
|
|
279
|
|
|
* @param mixed $condition |
|
|
|
|
|
|
280
|
|
|
* @return $this |
|
281
|
|
|
*/ |
|
282
|
|
|
public function wherePivot($field, $op = null, $condition = null) |
|
283
|
|
|
{ |
|
284
|
|
|
$this->query->where('pivot.' . $field, $op, $condition); |
|
285
|
|
|
return $this; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* 预载入关联查询(数据集) |
|
290
|
|
|
* @access public |
|
291
|
|
|
* @param array $resultSet 数据集 |
|
292
|
|
|
* @param string $relation 当前关联名 |
|
293
|
|
|
* @param string $subRelation 子关联名 |
|
294
|
|
|
* @param \Closure $closure 闭包 |
|
295
|
|
|
* @return void |
|
296
|
|
|
*/ |
|
297
|
|
|
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) |
|
298
|
|
|
{ |
|
299
|
|
|
$localKey = $this->localKey; |
|
300
|
|
|
$foreignKey = $this->foreignKey; |
|
|
|
|
|
|
301
|
|
|
|
|
302
|
|
|
$pk = $resultSet[0]->getPk(); |
|
303
|
|
|
$range = []; |
|
304
|
|
|
foreach ($resultSet as $result) { |
|
305
|
|
|
// 获取关联外键列表 |
|
306
|
|
|
if (isset($result->$pk)) { |
|
307
|
|
|
$range[] = $result->$pk; |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
if (!empty($range)) { |
|
312
|
|
|
// 查询关联数据 |
|
313
|
|
|
$data = $this->eagerlyManyToMany([ |
|
|
|
|
|
|
314
|
|
|
['pivot.' . $localKey, 'in', $range], |
|
315
|
|
|
], $relation, $subRelation, $closure); |
|
|
|
|
|
|
316
|
|
|
|
|
317
|
|
|
// 关联属性名 |
|
318
|
|
|
$attr = Loader::parseName($relation); |
|
319
|
|
|
|
|
320
|
|
|
// 关联数据封装 |
|
321
|
|
|
foreach ($resultSet as $result) { |
|
322
|
|
|
if (!isset($data[$result->$pk])) { |
|
323
|
|
|
$data[$result->$pk] = []; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
$result->setRelation($attr, $this->resultSetBuild($data[$result->$pk])); |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* 预载入关联查询(单个数据) |
|
333
|
|
|
* @access public |
|
334
|
|
|
* @param Model $result 数据对象 |
|
335
|
|
|
* @param string $relation 当前关联名 |
|
336
|
|
|
* @param string $subRelation 子关联名 |
|
337
|
|
|
* @param \Closure $closure 闭包 |
|
338
|
|
|
* @return void |
|
339
|
|
|
*/ |
|
340
|
|
|
public function eagerlyResult(&$result, $relation, $subRelation, $closure) |
|
341
|
|
|
{ |
|
342
|
|
|
$pk = $result->getPk(); |
|
343
|
|
|
|
|
344
|
|
|
if (isset($result->$pk)) { |
|
345
|
|
|
$pk = $result->$pk; |
|
346
|
|
|
// 查询管理数据 |
|
347
|
|
|
$data = $this->eagerlyManyToMany([ |
|
|
|
|
|
|
348
|
|
|
['pivot.' . $this->localKey, '=', $pk], |
|
349
|
|
|
], $relation, $subRelation, $closure); |
|
|
|
|
|
|
350
|
|
|
|
|
351
|
|
|
// 关联数据封装 |
|
352
|
|
|
if (!isset($data[$pk])) { |
|
353
|
|
|
$data[$pk] = []; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
$result->setRelation(Loader::parseName($relation), $this->resultSetBuild($data[$pk])); |
|
357
|
|
|
} |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* 关联统计 |
|
362
|
|
|
* @access public |
|
363
|
|
|
* @param Model $result 数据对象 |
|
|
|
|
|
|
364
|
|
|
* @param \Closure $closure 闭包 |
|
|
|
|
|
|
365
|
|
|
* @param string $aggregate 聚合查询方法 |
|
366
|
|
|
* @param string $field 字段 |
|
|
|
|
|
|
367
|
|
|
* @param string $name 统计字段别名 |
|
|
|
|
|
|
368
|
|
|
* @return integer |
|
369
|
|
|
*/ |
|
370
|
|
|
public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') |
|
371
|
|
|
{ |
|
372
|
|
|
$pk = $result->getPk(); |
|
373
|
|
|
|
|
374
|
|
|
if (!isset($result->$pk)) { |
|
375
|
|
|
return 0; |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
$pk = $result->$pk; |
|
379
|
|
|
|
|
380
|
|
|
if ($closure) { |
|
|
|
|
|
|
381
|
|
|
$return = $closure($this->query); |
|
382
|
|
|
|
|
383
|
|
|
if ($return && is_string($return)) { |
|
384
|
|
|
$name = $return; |
|
385
|
|
|
} |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
return $this->belongsToManyQuery($this->foreignKey, $this->localKey, [ |
|
|
|
|
|
|
389
|
|
|
['pivot.' . $this->localKey, '=', $pk], |
|
390
|
|
|
])->$aggregate($field); |
|
|
|
|
|
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
/** |
|
394
|
|
|
* 获取关联统计子查询 |
|
395
|
|
|
* @access public |
|
396
|
|
|
* @param \Closure $closure 闭包 |
|
|
|
|
|
|
397
|
|
|
* @param string $aggregate 聚合查询方法 |
|
|
|
|
|
|
398
|
|
|
* @param string $field 字段 |
|
|
|
|
|
|
399
|
|
|
* @param string $aggregateAlias 聚合字段别名 |
|
400
|
|
|
* @return array |
|
401
|
|
|
*/ |
|
402
|
|
|
public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') |
|
403
|
|
|
{ |
|
404
|
|
|
if ($closure) { |
|
|
|
|
|
|
405
|
|
|
$return = $closure($this->query); |
|
406
|
|
|
|
|
407
|
|
|
if ($return && is_string($return)) { |
|
408
|
|
|
$aggregateAlias = $return; |
|
409
|
|
|
} |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
return $this->belongsToManyQuery($this->foreignKey, $this->localKey, [ |
|
|
|
|
|
|
413
|
|
|
[ |
|
414
|
|
|
'pivot.' . $this->localKey, 'exp', $this->query->raw('=' . $this->parent->getTable() . '.' . $this->parent->getPk()), |
|
415
|
|
|
], |
|
416
|
|
|
])->fetchSql()->$aggregate($field); |
|
|
|
|
|
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
/** |
|
420
|
|
|
* 多对多 关联模型预查询 |
|
421
|
|
|
* @access protected |
|
422
|
|
|
* @param array $where 关联预查询条件 |
|
423
|
|
|
* @param string $relation 关联名 |
|
424
|
|
|
* @param string $subRelation 子关联 |
|
425
|
|
|
* @param \Closure $closure 闭包 |
|
426
|
|
|
* @return array |
|
427
|
|
|
*/ |
|
428
|
|
|
protected function eagerlyManyToMany($where, $relation, $subRelation = '', $closure = null) |
|
429
|
|
|
{ |
|
430
|
|
|
// 预载入关联查询 支持嵌套预载入 |
|
431
|
|
|
if ($closure) { |
|
432
|
|
|
$closure($this->query); |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
$list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where) |
|
436
|
|
|
->with($subRelation) |
|
437
|
|
|
->select(); |
|
438
|
|
|
|
|
439
|
|
|
// 组装模型数据 |
|
440
|
|
|
$data = []; |
|
441
|
|
|
foreach ($list as $set) { |
|
442
|
|
|
$pivot = []; |
|
443
|
|
|
foreach ($set->getData() as $key => $val) { |
|
444
|
|
|
if (strpos($key, '__')) { |
|
445
|
|
|
list($name, $attr) = explode('__', $key, 2); |
|
446
|
|
|
if ('pivot' == $name) { |
|
447
|
|
|
$pivot[$attr] = $val; |
|
448
|
|
|
unset($set->$key); |
|
449
|
|
|
} |
|
450
|
|
|
} |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
$set->setRelation($this->pivotDataName, $this->newPivot($pivot, true)); |
|
454
|
|
|
|
|
455
|
|
|
$data[$pivot[$this->localKey]][] = $set; |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
return $data; |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
|
|
/** |
|
462
|
|
|
* BELONGS TO MANY 关联查询 |
|
463
|
|
|
* @access protected |
|
464
|
|
|
* @param string $foreignKey 关联模型关联键 |
|
465
|
|
|
* @param string $localKey 当前模型关联键 |
|
466
|
|
|
* @param array $condition 关联查询条件 |
|
467
|
|
|
* @return Query |
|
468
|
|
|
*/ |
|
469
|
|
|
protected function belongsToManyQuery($foreignKey, $localKey, $condition = []) |
|
470
|
|
|
{ |
|
471
|
|
|
// 关联查询封装 |
|
472
|
|
|
$tableName = $this->query->getTable(); |
|
473
|
|
|
$table = $this->pivot->getTable(); |
|
|
|
|
|
|
474
|
|
|
$fields = $this->getQueryFields($tableName); |
|
475
|
|
|
|
|
476
|
|
|
$query = $this->query |
|
477
|
|
|
->field($fields) |
|
478
|
|
|
->field(true, false, $table, 'pivot', 'pivot__'); |
|
479
|
|
|
|
|
480
|
|
|
if (empty($this->baseQuery)) { |
|
481
|
|
|
$relationFk = $this->query->getPk(); |
|
482
|
|
|
$query->join([$table => 'pivot'], 'pivot.' . $foreignKey . '=' . $tableName . '.' . $relationFk) |
|
|
|
|
|
|
483
|
|
|
->where($condition); |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
return $query; |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
/** |
|
490
|
|
|
* 保存(新增)当前关联数据对象 |
|
491
|
|
|
* @access public |
|
492
|
|
|
* @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键 |
|
493
|
|
|
* @param array $pivot 中间表额外数据 |
|
494
|
|
|
* @return array|Pivot |
|
495
|
|
|
*/ |
|
496
|
|
|
public function save($data, array $pivot = []) |
|
497
|
|
|
{ |
|
498
|
|
|
// 保存关联表/中间表数据 |
|
499
|
|
|
return $this->attach($data, $pivot); |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
/** |
|
503
|
|
|
* 批量保存当前关联数据对象 |
|
504
|
|
|
* @access public |
|
505
|
|
|
* @param array $dataSet 数据集 |
|
506
|
|
|
* @param array $pivot 中间表额外数据 |
|
507
|
|
|
* @param bool $samePivot 额外数据是否相同 |
|
508
|
|
|
* @return array|false |
|
509
|
|
|
*/ |
|
510
|
|
|
public function saveAll(array $dataSet, array $pivot = [], $samePivot = false) |
|
511
|
|
|
{ |
|
512
|
|
|
$result = []; |
|
513
|
|
|
|
|
514
|
|
|
foreach ($dataSet as $key => $data) { |
|
515
|
|
|
if (!$samePivot) { |
|
516
|
|
|
$pivotData = isset($pivot[$key]) ? $pivot[$key] : []; |
|
517
|
|
|
} else { |
|
518
|
|
|
$pivotData = $pivot; |
|
519
|
|
|
} |
|
520
|
|
|
|
|
521
|
|
|
$result[] = $this->attach($data, $pivotData); |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
return empty($result) ? false : $result; |
|
525
|
|
|
} |
|
526
|
|
|
|
|
527
|
|
|
/** |
|
528
|
|
|
* 附加关联的一个中间表数据 |
|
529
|
|
|
* @access public |
|
530
|
|
|
* @param mixed $data 数据 可以使用数组、关联模型对象 或者 关联对象的主键 |
|
531
|
|
|
* @param array $pivot 中间表额外数据 |
|
532
|
|
|
* @return array|Pivot |
|
533
|
|
|
* @throws Exception |
|
534
|
|
|
*/ |
|
535
|
|
|
public function attach($data, $pivot = []) |
|
536
|
|
|
{ |
|
537
|
|
|
if (is_array($data)) { |
|
538
|
|
|
if (key($data) === 0) { |
|
539
|
|
|
$id = $data; |
|
540
|
|
|
} else { |
|
541
|
|
|
// 保存关联表数据 |
|
542
|
|
|
$model = new $this->model; |
|
543
|
|
|
$id = $model->insertGetId($data); |
|
544
|
|
|
} |
|
545
|
|
|
} elseif (is_numeric($data) || is_string($data)) { |
|
546
|
|
|
// 根据关联表主键直接写入中间表 |
|
547
|
|
|
$id = $data; |
|
548
|
|
|
} elseif ($data instanceof Model) { |
|
549
|
|
|
// 根据关联表主键直接写入中间表 |
|
550
|
|
|
$relationFk = $data->getPk(); |
|
551
|
|
|
$id = $data->$relationFk; |
|
552
|
|
|
} |
|
553
|
|
|
|
|
554
|
|
|
if ($id) { |
|
|
|
|
|
|
555
|
|
|
// 保存中间表数据 |
|
556
|
|
|
$pk = $this->parent->getPk(); |
|
557
|
|
|
$pivot[$this->localKey] = $this->parent->$pk; |
|
558
|
|
|
$ids = (array) $id; |
|
559
|
|
|
|
|
560
|
|
|
foreach ($ids as $id) { |
|
561
|
|
|
$pivot[$this->foreignKey] = $id; |
|
562
|
|
|
$this->pivot->replace() |
|
563
|
|
|
->exists(false) |
|
564
|
|
|
->data([]) |
|
565
|
|
|
->save($pivot); |
|
566
|
|
|
$result[] = $this->newPivot($pivot, true); |
|
567
|
|
|
} |
|
568
|
|
|
|
|
569
|
|
|
if (count($result) == 1) { |
|
|
|
|
|
|
570
|
|
|
// 返回中间表模型对象 |
|
571
|
|
|
$result = $result[0]; |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
|
|
return $result; |
|
575
|
|
|
} else { |
|
576
|
|
|
throw new Exception('miss relation data'); |
|
577
|
|
|
} |
|
578
|
|
|
} |
|
579
|
|
|
|
|
580
|
|
|
/** |
|
581
|
|
|
* 判断是否存在关联数据 |
|
582
|
|
|
* @access public |
|
583
|
|
|
* @param mixed $data 数据 可以使用关联模型对象 或者 关联对象的主键 |
|
|
|
|
|
|
584
|
|
|
* @return Pivot |
|
585
|
|
|
* @throws Exception |
|
586
|
|
|
*/ |
|
587
|
|
|
public function attached($data) |
|
588
|
|
|
{ |
|
589
|
|
|
if ($data instanceof Model) { |
|
590
|
|
|
$id = $data->getKey(); |
|
591
|
|
|
} else { |
|
592
|
|
|
$id = $data; |
|
593
|
|
|
} |
|
594
|
|
|
|
|
595
|
|
|
$pivot = $this->pivot |
|
596
|
|
|
->where($this->localKey, $this->parent->getKey()) |
|
597
|
|
|
->where($this->foreignKey, $id) |
|
598
|
|
|
->find(); |
|
599
|
|
|
|
|
600
|
|
|
return $pivot ?: false; |
|
601
|
|
|
} |
|
602
|
|
|
|
|
603
|
|
|
/** |
|
604
|
|
|
* 解除关联的一个中间表数据 |
|
605
|
|
|
* @access public |
|
606
|
|
|
* @param integer|array $data 数据 可以使用关联对象的主键 |
|
607
|
|
|
* @param bool $relationDel 是否同时删除关联表数据 |
|
608
|
|
|
* @return integer |
|
609
|
|
|
*/ |
|
610
|
|
|
public function detach($data = null, $relationDel = false) |
|
611
|
|
|
{ |
|
612
|
|
|
if (is_array($data)) { |
|
613
|
|
|
$id = $data; |
|
614
|
|
|
} elseif (is_numeric($data) || is_string($data)) { |
|
615
|
|
|
// 根据关联表主键直接写入中间表 |
|
616
|
|
|
$id = $data; |
|
617
|
|
|
} elseif ($data instanceof Model) { |
|
618
|
|
|
// 根据关联表主键直接写入中间表 |
|
619
|
|
|
$relationFk = $data->getPk(); |
|
620
|
|
|
$id = $data->$relationFk; |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
// 删除中间表数据 |
|
624
|
|
|
$pk = $this->parent->getPk(); |
|
625
|
|
|
$pivot[] = [$this->localKey, '=', $this->parent->$pk]; |
|
|
|
|
|
|
626
|
|
|
|
|
627
|
|
|
if (isset($id)) { |
|
628
|
|
|
$pivot[] = [$this->foreignKey, is_array($id) ? 'in' : '=', $id]; |
|
629
|
|
|
} |
|
630
|
|
|
|
|
631
|
|
|
$result = $this->pivot->where($pivot)->delete(); |
|
632
|
|
|
|
|
633
|
|
|
// 删除关联表数据 |
|
634
|
|
|
if (isset($id) && $relationDel) { |
|
635
|
|
|
$model = $this->model; |
|
636
|
|
|
$model::destroy($id); |
|
637
|
|
|
} |
|
638
|
|
|
|
|
639
|
|
|
return $result; |
|
640
|
|
|
} |
|
641
|
|
|
|
|
642
|
|
|
/** |
|
643
|
|
|
* 数据同步 |
|
644
|
|
|
* @access public |
|
645
|
|
|
* @param array $ids |
|
|
|
|
|
|
646
|
|
|
* @param bool $detaching |
|
|
|
|
|
|
647
|
|
|
* @return array |
|
648
|
|
|
*/ |
|
649
|
|
|
public function sync($ids, $detaching = true) |
|
650
|
|
|
{ |
|
651
|
|
|
$changes = [ |
|
652
|
|
|
'attached' => [], |
|
653
|
|
|
'detached' => [], |
|
654
|
|
|
'updated' => [], |
|
655
|
|
|
]; |
|
656
|
|
|
|
|
657
|
|
|
$pk = $this->parent->getPk(); |
|
658
|
|
|
|
|
659
|
|
|
$current = $this->pivot |
|
660
|
|
|
->where($this->localKey, $this->parent->$pk) |
|
661
|
|
|
->column($this->foreignKey); |
|
662
|
|
|
|
|
663
|
|
|
$records = []; |
|
664
|
|
|
|
|
665
|
|
|
foreach ($ids as $key => $value) { |
|
666
|
|
|
if (!is_array($value)) { |
|
667
|
|
|
$records[$value] = []; |
|
668
|
|
|
} else { |
|
669
|
|
|
$records[$key] = $value; |
|
670
|
|
|
} |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
|
|
$detach = array_diff($current, array_keys($records)); |
|
674
|
|
|
|
|
675
|
|
|
if ($detaching && count($detach) > 0) { |
|
676
|
|
|
$this->detach($detach); |
|
677
|
|
|
$changes['detached'] = $detach; |
|
678
|
|
|
} |
|
679
|
|
|
|
|
680
|
|
|
foreach ($records as $id => $attributes) { |
|
681
|
|
|
if (!in_array($id, $current)) { |
|
682
|
|
|
$this->attach($id, $attributes); |
|
683
|
|
|
$changes['attached'][] = $id; |
|
684
|
|
|
} elseif (count($attributes) > 0 && $this->attach($id, $attributes)) { |
|
685
|
|
|
$changes['updated'][] = $id; |
|
686
|
|
|
} |
|
687
|
|
|
} |
|
688
|
|
|
|
|
689
|
|
|
return $changes; |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
/** |
|
693
|
|
|
* 执行基础查询(仅执行一次) |
|
694
|
|
|
* @access protected |
|
695
|
|
|
* @return void |
|
696
|
|
|
*/ |
|
697
|
|
|
protected function baseQuery() |
|
698
|
|
|
{ |
|
699
|
|
|
if (empty($this->baseQuery) && $this->parent->getData()) { |
|
700
|
|
|
$pk = $this->parent->getPk(); |
|
701
|
|
|
$table = $this->pivot->getTable(); |
|
702
|
|
|
|
|
703
|
|
|
$this->query |
|
704
|
|
|
->join([$table => 'pivot'], 'pivot.' . $this->foreignKey . '=' . $this->query->getTable() . '.' . $this->query->getPk()) |
|
|
|
|
|
|
705
|
|
|
->where('pivot.' . $this->localKey, $this->parent->$pk); |
|
706
|
|
|
$this->baseQuery = true; |
|
707
|
|
|
} |
|
708
|
|
|
} |
|
709
|
|
|
|
|
710
|
|
|
} |
|
711
|
|
|
|