MergeModel.class.php
14 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
<?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 MergeModel extends Model
{
protected $modelList = array(); // 包含的模型列表 第一个必须是主表模型
protected $masterModel = ''; // 主模型
protected $joinType = 'INNER'; // 聚合模型的查询JOIN类型
protected $fk = ''; // 外键名 默认为主表名_id
protected $mapFields = array(); // 需要处理的模型映射字段,避免混淆 array( id => 'user.id' )
/**
* 架构函数
* 取得DB类的实例对象 字段检查
*
* @access public
* @param string $name
* 模型名称
* @param string $tablePrefix
* 表前缀
* @param mixed $connection
* 数据库连接信息
*/
public function __construct($name = '', $tablePrefix = '', $connection = '')
{
parent::__construct($name, $tablePrefix, $connection);
// 聚合模型的字段信息
if (empty($this->fields) && ! empty($this->modelList)) {
$fields = array();
foreach ($this->modelList as $model) {
// 获取模型的字段信息
$result = $this->db->getFields(M($model)->getTableName());
$_fields = array_keys($result);
// $this->mapFields = array_intersect($fields,$_fields);
$fields = array_merge($fields, $_fields);
}
$this->fields = $fields;
}
// 设置第一个模型为主表模型
if (empty($this->masterModel) && ! empty($this->modelList)) {
$this->masterModel = $this->modelList[0];
}
// 主表的主键名
$this->pk = M($this->masterModel)->getPk();
// 设置默认外键名 仅支持单一外键
if (empty($this->fk)) {
$this->fk = strtolower($this->masterModel) . '_id';
}
}
/**
* 得到完整的数据表名
*
* @access public
* @return string
*/
public function getTableName()
{
if (empty($this->trueTableName)) {
$tableName = array();
$models = $this->modelList;
foreach ($models as $model) {
$tableName[] = M($model)->getTableName() . ' ' . $model;
}
$this->trueTableName = implode(',', $tableName);
}
return $this->trueTableName;
}
/**
* 自动检测数据表信息
*
* @access protected
* @return void
*/
protected function _checkTableInfo()
{}
/**
* 新增聚合数据
*
* @access public
* @param mixed $data
* 数据
* @param array $options
* 表达式
* @param boolean $replace
* 是否replace
* @return mixed
*/
public function add($data = '', $options = array(), $replace = false)
{
if (empty($data)) {
// 没有传递数据,获取当前数据对象的值
if (! empty($this->data)) {
$data = $this->data;
// 重置数据
$this->data = array();
} else {
$this->error = L('_DATA_TYPE_INVALID_');
return false;
}
}
// 启动事务
$this->startTrans();
// 写入主表数据
$result = M($this->masterModel)->strict(false)->add($data);
if ($result) {
// 写入外键数据
$data[$this->fk] = $result;
$models = $this->modelList;
array_shift($models);
// 写入附表数据
foreach ($models as $model) {
$res = M($model)->strict(false)->add($data);
if (! $res) {
$this->rollback();
return false;
}
}
// 提交事务
$this->commit();
} else {
$this->rollback();
return false;
}
return $result;
}
/**
* 对保存到数据库的数据进行处理
*
* @access protected
* @param mixed $data
* 要操作的数据
* @return boolean
*/
protected function _facade($data)
{
// 检查数据字段合法性
if (! empty($this->fields)) {
if (! empty($this->options['field'])) {
$fields = $this->options['field'];
unset($this->options['field']);
if (is_string($fields)) {
$fields = explode(',', $fields);
}
} else {
$fields = $this->fields;
}
foreach ($data as $key => $val) {
if (! in_array($key, $fields, true)) {
unset($data[$key]);
} elseif (array_key_exists($key, $this->mapFields)) {
// 需要处理映射字段
$data[$this->mapFields[$key]] = $val;
unset($data[$key]);
}
}
}
// 安全过滤
if (! empty($this->options['filter'])) {
$data = array_map($this->options['filter'], $data);
unset($this->options['filter']);
}
$this->_before_write($data);
return $data;
}
/**
* 保存聚合模型数据
*
* @access public
* @param mixed $data
* 数据
* @param array $options
* 表达式
* @return boolean
*/
public function save($data = '', $options = array())
{
// 根据主表的主键更新
if (empty($data)) {
// 没有传递数据,获取当前数据对象的值
if (! empty($this->data)) {
$data = $this->data;
// 重置数据
$this->data = array();
} else {
$this->error = L('_DATA_TYPE_INVALID_');
return false;
}
}
if (empty($data)) {
// 没有数据则不执行
$this->error = L('_DATA_TYPE_INVALID_');
return false;
}
// 如果存在主键数据 则自动作为更新条件
$pk = $this->pk;
if (isset($data[$pk])) {
$where[$pk] = $data[$pk];
$options['where'] = $where;
unset($data[$pk]);
}
$options['join'] = '';
$options = $this->_parseOptions($options);
// 更新操作不使用JOIN
$options['table'] = $this->getTableName();
if (is_array($options['where']) && isset($options['where'][$pk])) {
$pkValue = $options['where'][$pk];
}
if (false === $this->_before_update($data, $options)) {
return false;
}
$result = $this->db->update($data, $options);
if (false !== $result) {
if (isset($pkValue)) {
$data[$pk] = $pkValue;
}
$this->_after_update($data, $options);
}
return $result;
}
/**
* 删除聚合模型数据
*
* @access public
* @param mixed $options
* 表达式
* @return mixed
*/
public function delete($options = array())
{
$pk = $this->pk;
if (empty($options) && empty($this->options['where'])) {
// 如果删除条件为空 则删除当前数据对象所对应的记录
if (! empty($this->data) && isset($this->data[$pk])) {
return $this->delete($this->data[$pk]);
} else {
return false;
}
}
if (is_numeric($options) || is_string($options)) {
// 根据主键删除记录
if (strpos($options, ',')) {
$where[$pk] = array(
'IN',
$options
);
} else {
$where[$pk] = $options;
}
$options = array();
$options['where'] = $where;
}
// 分析表达式
$options['join'] = '';
$options = $this->_parseOptions($options);
if (empty($options['where'])) {
// 如果条件为空 不进行删除操作 除非设置 1=1
return false;
}
if (is_array($options['where']) && isset($options['where'][$pk])) {
$pkValue = $options['where'][$pk];
}
$options['table'] = implode(',', $this->modelList);
$options['using'] = $this->getTableName();
if (false === $this->_before_delete($options)) {
return false;
}
$result = $this->db->delete($options);
if (false !== $result) {
$data = array();
if (isset($pkValue)) {
$data[$pk] = $pkValue;
}
$this->_after_delete($data, $options);
}
// 返回删除记录个数
return $result;
}
/**
* 表达式过滤方法
*
* @access protected
* @param string $options
* 表达式
* @return void
*/
protected function _options_filter(&$options)
{
if (! isset($options['join'])) {
$models = $this->modelList;
array_shift($models);
foreach ($models as $model) {
$options['join'][] = $this->joinType . ' JOIN ' . M($model)->getTableName() . ' ' . $model . ' ON ' . $this->masterModel . '.' . $this->pk . ' = ' . $model . '.' . $this->fk;
}
}
$options['table'] = M($this->masterModel)->getTableName() . ' ' . $this->masterModel;
$options['field'] = $this->checkFields(isset($options['field']) ? $options['field'] : '');
if (isset($options['group'])) {
$options['group'] = $this->checkGroup($options['group']);
}
if (isset($options['where'])) {
$options['where'] = $this->checkCondition($options['where']);
}
if (isset($options['order'])) {
$options['order'] = $this->checkOrder($options['order']);
}
}
/**
* 检查条件中的聚合字段
*
* @access protected
* @param mixed $data
* 条件表达式
* @return array
*/
protected function checkCondition($where)
{
if (is_array($where)) {
$view = array();
foreach ($where as $name => $value) {
if (array_key_exists($name, $this->mapFields)) {
// 需要处理映射字段
$view[$this->mapFields[$name]] = $value;
unset($where[$name]);
}
}
$where = array_merge($where, $view);
}
return $where;
}
/**
* 检查Order表达式中的聚合字段
*
* @access protected
* @param string $order
* 字段
* @return string
*/
protected function checkOrder($order = '')
{
if (is_string($order) && ! empty($order)) {
$orders = explode(',', $order);
$_order = array();
foreach ($orders as $order) {
$array = explode(' ', trim($order));
$field = $array[0];
$sort = isset($array[1]) ? $array[1] : 'ASC';
if (array_key_exists($field, $this->mapFields)) {
// 需要处理映射字段
$field = $this->mapFields[$field];
}
$_order[] = $field . ' ' . $sort;
}
$order = implode(',', $_order);
}
return $order;
}
/**
* 检查Group表达式中的聚合字段
*
* @access protected
* @param string $group
* 字段
* @return string
*/
protected function checkGroup($group = '')
{
if (! empty($group)) {
$groups = explode(',', $group);
$_group = array();
foreach ($groups as $field) {
// 解析成聚合字段
if (array_key_exists($field, $this->mapFields)) {
// 需要处理映射字段
$field = $this->mapFields[$field];
}
$_group[] = $field;
}
$group = implode(',', $_group);
}
return $group;
}
/**
* 检查fields表达式中的聚合字段
*
* @access protected
* @param string $fields
* 字段
* @return string
*/
protected function checkFields($fields = '')
{
if (empty($fields) || '*' == $fields) {
// 获取全部聚合字段
$fields = $this->fields;
}
if (! is_array($fields)) {
$fields = explode(',', $fields);
}
// 解析成聚合字段
$array = array();
foreach ($fields as $field) {
if (array_key_exists($field, $this->mapFields)) {
// 需要处理映射字段
$array[] = $this->mapFields[$field] . ' AS ' . $field;
} else {
$array[] = $field;
}
}
$fields = implode(',', $array);
return $fields;
}
/**
* 获取数据表字段信息
*
* @access public
* @return array
*/
public function getDbFields()
{
return $this->fields;
}
}