RelationModel.class.php
22.5 KB
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
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Think\Model;
use Think\Model;
/**
* ThinkPHP关联模型扩展
*/
class RelationModel extends Model
{
const HAS_ONE = 1;
const BELONGS_TO = 2;
const HAS_MANY = 3;
const MANY_TO_MANY = 4;
// 关联定义
protected $_link = array();
/**
* 动态方法实现
*
* @access public
* @param string $method
* 方法名称
* @param array $args
* 调用参数
* @return mixed
*/
public function __call($method, $args)
{
if (strtolower(substr($method, 0, 8)) == 'relation') {
$type = strtoupper(substr($method, 8));
if (in_array($type, array(
'ADD',
'SAVE',
'DEL'
), true)) {
array_unshift($args, $type);
return call_user_func_array(array(
&$this,
'opRelation'
), $args);
}
} else {
return parent::__call($method, $args);
}
}
/**
* 得到关联的数据表名
*
* @access public
* @return string
*/
public function getRelationTableName($relation)
{
$relationTable = ! empty($this->tablePrefix) ? $this->tablePrefix : '';
$relationTable .= $this->tableName ? $this->tableName : $this->name;
$relationTable .= '_' . $relation->getModelName();
return strtolower($relationTable);
}
// 查询成功后的回调方法
protected function _after_find(&$result, $options)
{
// 获取关联数据 并附加到结果中
if (! empty($options['link'])) {
$this->getRelation($result, $options['link']);
}
}
// 查询数据集成功后的回调方法
protected function _after_select(&$result, $options)
{
// 获取关联数据 并附加到结果中
if (! empty($options['link'])) {
$this->getRelations($result, $options['link']);
}
}
// 写入成功后的回调方法
protected function _after_insert($data, $options)
{
// 关联写入
if (! empty($options['link'])) {
$this->opRelation('ADD', $data, $options['link']);
}
}
// 更新成功后的回调方法
protected function _after_update($data, $options)
{
// 关联更新
if (! empty($options['link'])) {
$this->opRelation('SAVE', $data, $options['link']);
}
}
// 删除成功后的回调方法
protected function _after_delete($data, $options)
{
// 关联删除
if (! empty($options['link'])) {
$this->opRelation('DEL', $data, $options['link']);
}
}
/**
* 对保存到数据库的数据进行处理
*
* @access protected
* @param mixed $data
* 要操作的数据
* @return boolean
*/
protected function _facade($data)
{
$this->_before_write($data);
return $data;
}
/**
* 获取返回数据集的关联记录
*
* @access protected
* @param array $resultSet
* 返回数据
* @param string|array $name
* 关联名称
* @return array
*/
protected function getRelations(&$resultSet, $name = '')
{
// 获取记录集的主键列表
foreach ($resultSet as $key => $val) {
$val = $this->getRelation($val, $name);
$resultSet[$key] = $val;
}
return $resultSet;
}
/**
* 获取返回数据的关联记录
*
* @access protected
* @param mixed $result
* 返回数据
* @param string|array $name
* 关联名称
* @param boolean $return
* 是否返回关联数据本身
* @return array
*/
protected function getRelation(&$result, $name = '', $return = false)
{
if (! empty($this->_link)) {
foreach ($this->_link as $key => $val) {
$mappingName = ! empty($val['mapping_name']) ? $val['mapping_name'] : $key; // 映射名称
if (empty($name) || true === $name || $mappingName == $name || (is_array($name) && in_array($mappingName, $name))) {
$mappingType = ! empty($val['mapping_type']) ? $val['mapping_type'] : $val; // 关联类型
$mappingClass = ! empty($val['class_name']) ? $val['class_name'] : $key; // 关联类名
$mappingFields = ! empty($val['mapping_fields']) ? $val['mapping_fields'] : '*'; // 映射字段
$mappingCondition = ! empty($val['condition']) ? $val['condition'] : '1=1'; // 关联条件
$mappingKey = ! empty($val['mapping_key']) ? $val['mapping_key'] : $this->getPk(); // 关联键名
if (strtoupper($mappingClass) == strtoupper($this->name)) {
// 自引用关联 获取父键名
$mappingFk = ! empty($val['parent_key']) ? $val['parent_key'] : 'parent_id';
} else {
$mappingFk = ! empty($val['foreign_key']) ? $val['foreign_key'] : strtolower($this->name) . '_id'; // 关联外键
}
// 获取关联模型对象
$model = D($mappingClass);
switch ($mappingType) {
case self::HAS_ONE:
$pk = $result[$mappingKey];
$mappingCondition .= " AND {$mappingFk}='{$pk}'";
$relationData = $model->where($mappingCondition)
->field($mappingFields)
->find();
if (! empty($val['relation_deep'])) {
$model->getRelation($relationData, $val['relation_deep']);
}
break;
case self::BELONGS_TO:
if (strtoupper($mappingClass) == strtoupper($this->name)) {
// 自引用关联 获取父键名
$mappingFk = ! empty($val['parent_key']) ? $val['parent_key'] : 'parent_id';
} else {
$mappingFk = ! empty($val['foreign_key']) ? $val['foreign_key'] : strtolower($model->getModelName()) . '_id'; // 关联外键
}
$fk = $result[$mappingFk];
$mappingCondition .= " AND {$model->getPk()}='{$fk}'";
$relationData = $model->where($mappingCondition)
->field($mappingFields)
->find();
if (! empty($val['relation_deep'])) {
$model->getRelation($relationData, $val['relation_deep']);
}
break;
case self::HAS_MANY:
$pk = $result[$mappingKey];
$mappingCondition .= " AND {$mappingFk}='{$pk}'";
$mappingOrder = ! empty($val['mapping_order']) ? $val['mapping_order'] : '';
$mappingLimit = ! empty($val['mapping_limit']) ? $val['mapping_limit'] : '';
// 延时获取关联记录
$relationData = $model->where($mappingCondition)
->field($mappingFields)
->order($mappingOrder)
->limit($mappingLimit)
->select();
if (! empty($val['relation_deep'])) {
foreach ($relationData as $key => $data) {
$model->getRelation($data, $val['relation_deep']);
$relationData[$key] = $data;
}
}
break;
case self::MANY_TO_MANY:
$pk = $result[$mappingKey];
$prefix = $this->tablePrefix;
$mappingCondition = " {$mappingFk}='{$pk}'";
$mappingOrder = $val['mapping_order'];
$mappingLimit = $val['mapping_limit'];
$mappingRelationFk = $val['relation_foreign_key'] ? $val['relation_foreign_key'] : $model->getModelName() . '_id';
if (isset($val['relation_table'])) {
$mappingRelationTable = preg_replace_callback("/__([A-Z_-]+)__/sU", function ($match) use($prefix)
{
return $prefix . strtolower($match[1]);
}, $val['relation_table']);
} else {
$mappingRelationTable = $this->getRelationTableName($model);
}
$sql = "SELECT b.{$mappingFields} FROM {$mappingRelationTable} AS a, " . $model->getTableName() . " AS b WHERE a.{$mappingRelationFk} = b.{$model->getPk()} AND a.{$mappingCondition}";
if (! empty($val['condition'])) {
$sql .= ' AND ' . $val['condition'];
}
if (! empty($mappingOrder)) {
$sql .= ' ORDER BY ' . $mappingOrder;
}
if (! empty($mappingLimit)) {
$sql .= ' LIMIT ' . $mappingLimit;
}
$relationData = $this->query($sql);
if (! empty($val['relation_deep'])) {
foreach ($relationData as $key => $data) {
$model->getRelation($data, $val['relation_deep']);
$relationData[$key] = $data;
}
}
break;
}
if (! $return) {
if (isset($val['as_fields']) && in_array($mappingType, array(
self::HAS_ONE,
self::BELONGS_TO
))) {
// 支持直接把关联的字段值映射成数据对象中的某个字段
// 仅仅支持HAS_ONE BELONGS_TO
$fields = explode(',', $val['as_fields']);
foreach ($fields as $field) {
if (strpos($field, ':')) {
list ($relationName, $nick) = explode(':', $field);
$result[$nick] = $relationData[$relationName];
} else {
$result[$field] = $relationData[$field];
}
}
} else {
$result[$mappingName] = $relationData;
}
unset($relationData);
} else {
return $relationData;
}
}
}
}
return $result;
}
/**
* 操作关联数据
*
* @access protected
* @param string $opType
* 操作方式 ADD SAVE DEL
* @param mixed $data
* 数据对象
* @param string $name
* 关联名称
* @return mixed
*/
protected function opRelation($opType, $data = '', $name = '')
{
$result = false;
if (empty($data) && ! empty($this->data)) {
$data = $this->data;
} elseif (! is_array($data)) {
// 数据无效返回
return false;
}
if (! empty($this->_link)) {
// 遍历关联定义
foreach ($this->_link as $key => $val) {
// 操作制定关联类型
$mappingName = $val['mapping_name'] ? $val['mapping_name'] : $key; // 映射名称
if (empty($name) || true === $name || $mappingName == $name || (is_array($name) && in_array($mappingName, $name))) {
// 操作制定的关联
$mappingType = ! empty($val['mapping_type']) ? $val['mapping_type'] : $val; // 关联类型
$mappingClass = ! empty($val['class_name']) ? $val['class_name'] : $key; // 关联类名
$mappingKey = ! empty($val['mapping_key']) ? $val['mapping_key'] : $this->getPk(); // 关联键名
// 当前数据对象主键值
$pk = $data[$mappingKey];
if (strtoupper($mappingClass) == strtoupper($this->name)) {
// 自引用关联 获取父键名
$mappingFk = ! empty($val['parent_key']) ? $val['parent_key'] : 'parent_id';
} else {
$mappingFk = ! empty($val['foreign_key']) ? $val['foreign_key'] : strtolower($this->name) . '_id'; // 关联外键
}
if (! empty($val['condition'])) {
$mappingCondition = $val['condition'];
} else {
$mappingCondition = array();
$mappingCondition[$mappingFk] = $pk;
}
// 获取关联model对象
$model = D($mappingClass);
$mappingData = isset($data[$mappingName]) ? $data[$mappingName] : false;
if (! empty($mappingData) || $opType == 'DEL') {
switch ($mappingType) {
case self::HAS_ONE:
switch (strtoupper($opType)) {
case 'ADD': // 增加关联数据
$mappingData[$mappingFk] = $pk;
$result = $model->add($mappingData);
break;
case 'SAVE': // 更新关联数据
$result = $model->where($mappingCondition)->save($mappingData);
break;
case 'DEL': // 根据外键删除关联数据
$result = $model->where($mappingCondition)->delete();
break;
}
break;
case self::BELONGS_TO:
break;
case self::HAS_MANY:
switch (strtoupper($opType)) {
case 'ADD': // 增加关联数据
$model->startTrans();
foreach ($mappingData as $val) {
$val[$mappingFk] = $pk;
$result = $model->add($val);
}
$model->commit();
break;
case 'SAVE': // 更新关联数据
$model->startTrans();
$pk = $model->getPk();
foreach ($mappingData as $vo) {
if (isset($vo[$pk])) { // 更新数据
$mappingCondition = "$pk ={$vo[$pk]}";
$result = $model->where($mappingCondition)->save($vo);
} else { // 新增数据
$vo[$mappingFk] = $data[$mappingKey];
$result = $model->add($vo);
}
}
$model->commit();
break;
case 'DEL': // 删除关联数据
$result = $model->where($mappingCondition)->delete();
break;
}
break;
case self::MANY_TO_MANY:
$mappingRelationFk = $val['relation_foreign_key'] ? $val['relation_foreign_key'] : $model->getModelName() . '_id'; // 关联
$prefix = $this->tablePrefix;
if (isset($val['relation_table'])) {
$mappingRelationTable = preg_replace_callback("/__([A-Z_-]+)__/sU", function ($match) use($prefix)
{
return $prefix . strtolower($match[1]);
}, $val['relation_table']);
} else {
$mappingRelationTable = $this->getRelationTableName($model);
}
if (is_array($mappingData)) {
$ids = array();
foreach ($mappingData as $vo) {
$ids[] = $vo[$mappingKey];
}
$relationId = implode(',', $ids);
}
switch (strtoupper($opType)) {
case 'ADD': // 增加关联数据
if (isset($relationId)) {
$this->startTrans();
// 插入关联表数据
$sql = 'INSERT INTO ' . $mappingRelationTable . ' (' . $mappingFk . ',' . $mappingRelationFk . ') SELECT a.' . $this->getPk() . ',b.' . $model->getPk() . ' FROM ' . $this->getTableName() . ' AS a ,' . $model->getTableName() . " AS b where a." . $this->getPk() . ' =' . $pk . ' AND b.' . $model->getPk() . ' IN (' . $relationId . ") ";
$result = $model->execute($sql);
if (false !== $result) // 提交事务
{
$this->commit();
} else // 事务回滚
{
$this->rollback();
}
}
break;
case 'SAVE': // 更新关联数据
if (isset($relationId)) {
$this->startTrans();
// 删除关联表数据
$this->table($mappingRelationTable)
->where($mappingCondition)
->delete();
// 插入关联表数据
$sql = 'INSERT INTO ' . $mappingRelationTable . ' (' . $mappingFk . ',' . $mappingRelationFk . ') SELECT a.' . $this->getPk() . ',b.' . $model->getPk() . ' FROM ' . $this->getTableName() . ' AS a ,' . $model->getTableName() . " AS b where a." . $this->getPk() . ' =' . $pk . ' AND b.' . $model->getPk() . ' IN (' . $relationId . ") ";
$result = $model->execute($sql);
if (false !== $result) // 提交事务
{
$this->commit();
} else // 事务回滚
{
$this->rollback();
}
}
break;
case 'DEL': // 根据外键删除中间表关联数据
$result = $this->table($mappingRelationTable)
->where($mappingCondition)
->delete();
break;
}
break;
}
if (! empty($val['relation_deep'])) {
$model->opRelation($opType, $mappingData, $val['relation_deep']);
}
}
}
}
}
return $result;
}
/**
* 进行关联查询
*
* @access public
* @param mixed $name
* 关联名称
* @return Model
*/
public function relation($name)
{
$this->options['link'] = $name;
return $this;
}
/**
* 关联数据获取 仅用于查询后
*
* @access public
* @param string $name
* 关联名称
* @return array
*/
public function relationGet($name)
{
if (empty($this->data)) {
return false;
}
return $this->getRelation($this->data, $name, true);
}
}