|
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; |
|
13
|
|
|
|
|
14
|
|
|
use think\db\Query; |
|
15
|
|
|
use think\Exception; |
|
16
|
|
|
use think\Model; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Relation |
|
20
|
|
|
* @package think\model |
|
|
|
|
|
|
21
|
|
|
* |
|
22
|
|
|
* @mixin Query |
|
23
|
|
|
*/ |
|
|
|
|
|
|
24
|
|
|
abstract class Relation |
|
25
|
|
|
{ |
|
26
|
|
|
// 父模型对象 |
|
27
|
|
|
protected $parent; |
|
28
|
|
|
/** @var Model 当前关联的模型类 */ |
|
|
|
|
|
|
29
|
|
|
protected $model; |
|
30
|
|
|
/** @var Query 关联模型查询对象 */ |
|
|
|
|
|
|
31
|
|
|
protected $query; |
|
32
|
|
|
// 关联表外键 |
|
33
|
|
|
protected $foreignKey; |
|
34
|
|
|
// 关联表主键 |
|
35
|
|
|
protected $localKey; |
|
36
|
|
|
// 基础查询 |
|
37
|
|
|
protected $baseQuery; |
|
38
|
|
|
// 是否为自关联 |
|
39
|
|
|
protected $selfRelation; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* 获取关联的所属模型 |
|
43
|
|
|
* @access public |
|
44
|
|
|
* @return Model |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getParent() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->parent; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* 获取当前的关联模型类的实例 |
|
53
|
|
|
* @access public |
|
54
|
|
|
* @return Model |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getModel() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->query->getModel(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* 获取当前的关联模型类的实例 |
|
63
|
|
|
* @access public |
|
64
|
|
|
* @return Query |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getQuery() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->query; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* 设置当前关联为自关联 |
|
73
|
|
|
* @access public |
|
74
|
|
|
* @param bool $self 是否自关联 |
|
75
|
|
|
* @return $this |
|
76
|
|
|
*/ |
|
77
|
|
|
public function selfRelation($self = true) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->selfRelation = $self; |
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* 当前关联是否为自关联 |
|
85
|
|
|
* @access public |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
public function isSelfRelation() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->selfRelation; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* 封装关联数据集 |
|
95
|
|
|
* @access public |
|
96
|
|
|
* @param array $resultSet 数据集 |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function resultSetBuild($resultSet) |
|
100
|
|
|
{ |
|
101
|
|
|
return (new $this->model)->toCollection($resultSet); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function getQueryFields($model) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
$fields = $this->query->getOptions('field'); |
|
107
|
|
|
return $this->getRelationQueryFields($fields, $model); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
protected function getRelationQueryFields($fields, $model) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
if ($fields) { |
|
113
|
|
|
|
|
114
|
|
|
if (is_string($fields)) { |
|
115
|
|
|
$fields = explode(',', $fields); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
foreach ($fields as &$field) { |
|
119
|
|
|
if (false === strpos($field, '.')) { |
|
120
|
|
|
$field = $model . '.' . $field; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} else { |
|
124
|
|
|
$fields = $model . '.*'; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $fields; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
protected function getQueryWhere(&$where, $relation) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
foreach ($where as $key => &$val) { |
|
133
|
|
|
if (is_string($key)) { |
|
134
|
|
|
$where[] = [false === strpos($key, '.') ? $relation . '.' . $key : $key, '=', $val]; |
|
135
|
|
|
unset($where[$key]); |
|
136
|
|
|
} elseif (isset($val[0]) && false === strpos($val[0], '.')) { |
|
137
|
|
|
$val[0] = $relation . '.' . $val[0]; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* 更新数据 |
|
144
|
|
|
* @access public |
|
145
|
|
|
* @param array $data 更新数据 |
|
146
|
|
|
* @return integer|string |
|
147
|
|
|
*/ |
|
148
|
|
|
public function update(array $data = []) |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->query->update($data); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* 删除记录 |
|
155
|
|
|
* @access public |
|
156
|
|
|
* @param mixed $data 表达式 true 表示强制删除 |
|
157
|
|
|
* @return int |
|
158
|
|
|
* @throws Exception |
|
159
|
|
|
* @throws PDOException |
|
160
|
|
|
*/ |
|
161
|
|
|
public function delete($data = null) |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->query->delete($data); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* 执行基础查询(仅执行一次) |
|
168
|
|
|
* @access protected |
|
169
|
|
|
* @return void |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function baseQuery() |
|
172
|
|
|
{} |
|
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
public function __call($method, $args) |
|
|
|
|
|
|
175
|
|
|
{ |
|
176
|
|
|
if ($this->query) { |
|
177
|
|
|
// 执行基础查询 |
|
178
|
|
|
$this->baseQuery(); |
|
179
|
|
|
|
|
180
|
|
|
$result = call_user_func_array([$this->query->getModel(), $method], $args); |
|
181
|
|
|
|
|
182
|
|
|
return $result === $this->query && !in_array(strtolower($method), ['fetchsql', 'fetchpdo']) ? $this : $result; |
|
183
|
|
|
} else { |
|
184
|
|
|
throw new Exception('method not exists:' . __CLASS__ . '->' . $method); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|