Completed
Push — 6.0 ( 41214e...845f81 )
by liu
06:11 queued 01:15
created

Relation::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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\model;
14
15
use think\db\Query;
16
use think\Exception;
17
use think\Model;
18
19
/**
20
 * 模型关联基础类
21
 * @package think\model
0 ignored issues
show
Coding Style introduced by
Package name "think\model" is not valid; consider "Thinkmodel" instead
Loading history...
22
 *
23
 * @mixin Query
24
 */
4 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
25
abstract class Relation
26
{
27
    /**
28
     * 父模型对象
29
     * @var Model
30
     */
31
    protected $parent;
32
33
    /**
34
     * 当前关联的模型类名
35
     * @var string
36
     */
37
    protected $model;
38
39
    /**
40
     * 关联模型查询对象
41
     * @var Query
42
     */
43
    protected $query;
44
45
    /**
46
     * 关联表外键
47
     * @var string
48
     */
49
    protected $foreignKey;
50
51
    /**
52
     * 关联表主键
53
     * @var string
54
     */
55
    protected $localKey;
56
57
    /**
58
     * 是否执行关联基础查询
59
     * @var bool
60
     */
61
    protected $baseQuery;
62
63
    /**
64
     * 是否为自关联
65
     * @var bool
66
     */
67
    protected $selfRelation = false;
68
69
    /**
70
     * 关联数据数量限制
71
     * @var int
72
     */
73
    protected $withLimit;
74
75
    /**
76
     * 关联数据字段限制
77
     * @var array
78
     */
79
    protected $withField;
80
81
    /**
82
     * 获取关联的所属模型
83
     * @access public
84
     * @return Model
85
     */
86
    public function getParent(): Model
87
    {
88
        return $this->parent;
89
    }
90
91
    /**
92
     * 获取当前的关联模型类的Query实例
93
     * @access public
94
     * @return Query
95
     */
96
    public function getQuery()
97
    {
98
        return $this->query;
99
    }
100
101
    /**
102
     * 获取当前的关联模型类的实例
103
     * @access public
104
     * @return Model
105
     */
106
    public function getModel(): Model
107
    {
108
        return $this->query->getModel();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->query->getModel() could return the type null which is incompatible with the type-hinted return think\Model. Consider adding an additional type-check to rule them out.
Loading history...
109
    }
110
111
    /**
112
     * 当前关联是否为自关联
113
     * @access public
114
     * @return bool
115
     */
116
    public function isSelfRelation(): bool
117
    {
118
        return $this->selfRelation;
119
    }
120
121
    /**
122
     * 封装关联数据集
123
     * @access public
124
     * @param  array $resultSet 数据集
125
     * @param  Model $parent 父模型
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
126
     * @return mixed
127
     */
128
    protected function resultSetBuild(array $resultSet, Model $parent = null)
129
    {
130
        return (new $this->model)->toCollection($resultSet)->setParent($parent);
131
    }
132
133
    protected function getQueryFields(string $model)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getQueryFields()
Loading history...
134
    {
135
        $fields = $this->query->getOptions('field');
136
        return $this->getRelationQueryFields($fields, $model);
137
    }
138
139
    protected function getRelationQueryFields($fields, string $model)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRelationQueryFields()
Loading history...
140
    {
141
        if (empty($fields) || '*' == $fields) {
142
            return $model . '.*';
143
        }
144
145
        if (is_string($fields)) {
146
            $fields = explode(',', $fields);
147
        }
148
149
        foreach ($fields as &$field) {
150
            if (false === strpos($field, '.')) {
151
                $field = $model . '.' . $field;
152
            }
153
        }
154
155
        return $fields;
156
    }
157
158
    protected function getQueryWhere(array &$where, string $relation): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getQueryWhere()
Loading history...
159
    {
160
        foreach ($where as $key => &$val) {
161
            if (is_string($key)) {
162
                $where[] = [false === strpos($key, '.') ? $relation . '.' . $key : $key, '=', $val];
163
                unset($where[$key]);
164
            } elseif (isset($val[0]) && false === strpos($val[0], '.')) {
165
                $val[0] = $relation . '.' . $val[0];
166
            }
167
        }
168
    }
169
170
    /**
171
     * 更新数据
172
     * @access public
173
     * @param  array $data 更新数据
174
     * @return integer
175
     */
176
    public function update(array $data = []): int
177
    {
178
        return $this->query->update($data);
179
    }
180
181
    /**
182
     * 删除记录
183
     * @access public
184
     * @param  mixed $data 表达式 true 表示强制删除
185
     * @return int
186
     * @throws Exception
187
     * @throws PDOException
188
     */
189
    public function delete($data = null): int
190
    {
191
        return $this->query->delete($data);
192
    }
193
194
    /**
195
     * 限制关联数据的数量
196
     * @access public
197
     * @param  int $limit 关联数量限制
198
     * @return $this
199
     */
200
    public function withLimit(int $limit)
201
    {
202
        $this->withLimit = $limit;
203
        return $this;
204
    }
205
206
    /**
207
     * 限制关联数据的字段
208
     * @access public
209
     * @param  array $field 关联字段限制
210
     * @return $this
211
     */
212
    public function withField(array $field)
213
    {
214
        $this->withField = $field;
215
        return $this;
216
    }
217
218
    /**
219
     * 执行基础查询(仅执行一次)
220
     * @access protected
221
     * @return void
222
     */
223
    protected function baseQuery(): void
224
    {}
0 ignored issues
show
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
225
226
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
227
    {
228
        if ($this->query) {
229
            // 执行基础查询
230
            $this->baseQuery();
231
232
            $model  = $this->query->getModel(false);
233
            $result = call_user_func_array([$model, $method], $args);
234
235
            $this->query = $model->getQuery();
236
            return $result === $this->query ? $this : $result;
237
        }
238
239
        throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
240
    }
241
}
242