|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2019 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
|
|
|
declare (strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace think\db\concern; |
|
14
|
|
|
|
|
15
|
|
|
use think\db\Exception; |
|
16
|
|
|
|
|
17
|
|
|
trait UpdateCheck |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* 分析数据是否存在更新条件 |
|
21
|
|
|
* @access public |
|
22
|
|
|
* @param array $data 数据 |
|
23
|
|
|
* @return bool |
|
24
|
|
|
* @throws Exception |
|
25
|
|
|
*/ |
|
26
|
|
|
public function parseUpdateData(&$data): bool |
|
27
|
|
|
{ |
|
28
|
|
|
$pk = $this->getPk(); |
|
|
|
|
|
|
29
|
|
|
$isUpdate = false; |
|
30
|
|
|
// 如果存在主键数据 则自动作为更新条件 |
|
31
|
|
|
if (is_string($pk) && isset($data[$pk])) { |
|
32
|
|
|
$this->where($pk, '=', $data[$pk]); |
|
|
|
|
|
|
33
|
|
|
$this->options['key'] = $data[$pk]; |
|
|
|
|
|
|
34
|
|
|
unset($data[$pk]); |
|
35
|
|
|
$isUpdate = true; |
|
36
|
|
|
} elseif (is_array($pk)) { |
|
37
|
|
|
foreach ($pk as $field) { |
|
38
|
|
|
if (isset($data[$field])) { |
|
39
|
|
|
$this->where($field, '=', $data[$field]); |
|
40
|
|
|
$isUpdate = true; |
|
41
|
|
|
} else { |
|
42
|
|
|
// 如果缺少复合主键数据则不执行 |
|
43
|
|
|
throw new Exception('miss complex primary data'); |
|
44
|
|
|
} |
|
45
|
|
|
unset($data[$field]); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $isUpdate; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* 把主键值转换为查询条件 支持复合主键 |
|
54
|
|
|
* @access public |
|
55
|
|
|
* @param array|string $data 主键数据 |
|
56
|
|
|
* @return void |
|
57
|
|
|
* @throws Exception |
|
58
|
|
|
*/ |
|
59
|
|
|
public function parsePkWhere($data): void |
|
60
|
|
|
{ |
|
61
|
|
|
$pk = $this->getPk(); |
|
62
|
|
|
|
|
63
|
|
|
if (is_string($pk)) { |
|
64
|
|
|
// 获取数据表 |
|
65
|
|
|
if (empty($this->options['table'])) { |
|
66
|
|
|
$this->options['table'] = $this->getTable(); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$table = is_array($this->options['table']) ? key($this->options['table']) : $this->options['table']; |
|
70
|
|
|
|
|
71
|
|
|
if (!empty($this->options['alias'][$table])) { |
|
72
|
|
|
$alias = $this->options['alias'][$table]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$key = isset($alias) ? $alias . '.' . $pk : $pk; |
|
76
|
|
|
// 根据主键查询 |
|
77
|
|
|
if (is_array($data)) { |
|
78
|
|
|
$this->where($key, 'in', $data); |
|
79
|
|
|
} else { |
|
80
|
|
|
$this->where($key, '=', $data); |
|
81
|
|
|
$this->options['key'] = $data; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* 获取模型的更新条件 |
|
88
|
|
|
* @access protected |
|
89
|
|
|
* @param array $options 查询参数 |
|
90
|
|
|
*/ |
|
|
|
|
|
|
91
|
|
|
protected function getModelUpdateCondition(array $options) |
|
92
|
|
|
{ |
|
93
|
|
|
return $options['where']['AND'] ?? null; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|