Passed
Push — 6.0 ( 03c51c...0b31df )
by liu
02:38
created

Relation::resultSetBuild()   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 1
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;
68
69
    /**
70
     * 获取关联的所属模型
71
     * @access public
72
     * @return Model
73
     */
74
    public function getParent(): Model
75
    {
76
        return $this->parent;
77
    }
78
79
    /**
80
     * 获取当前的关联模型类的Query实例
81
     * @access public
82
     * @return Query
83
     */
84
    public function getQuery()
85
    {
86
        return $this->query;
87
    }
88
89
    /**
90
     * 获取当前的关联模型类的实例
91
     * @access public
92
     * @return Model
93
     */
94
    public function getModel(): Model
95
    {
96
        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...
97
    }
98
99
    /**
100
     * 当前关联是否为自关联
101
     * @access public
102
     * @return bool
103
     */
104
    public function isSelfRelation(): bool
105
    {
106
        return $this->selfRelation;
107
    }
108
109
    /**
110
     * 封装关联数据集
111
     * @access public
112
     * @param  array $resultSet 数据集
113
     * @return mixed
114
     */
115
    protected function resultSetBuild(array $resultSet)
116
    {
117
        return (new $this->model)->toCollection($resultSet);
118
    }
119
120
    protected function getQueryFields(string $model)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getQueryFields()
Loading history...
121
    {
122
        $fields = $this->query->getOptions('field');
123
        return $this->getRelationQueryFields($fields, $model);
124
    }
125
126
    protected function getRelationQueryFields($fields, string $model)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRelationQueryFields()
Loading history...
127
    {
128
        if (empty($fields) || '*' == $fields) {
129
            return $model . '.*';
130
        }
131
132
        if (is_string($fields)) {
133
            $fields = explode(',', $fields);
134
        }
135
136
        foreach ($fields as &$field) {
137
            if (false === strpos($field, '.')) {
138
                $field = $model . '.' . $field;
139
            }
140
        }
141
142
        return $fields;
143
    }
144
145
    protected function getQueryWhere(array &$where, string $relation): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getQueryWhere()
Loading history...
146
    {
147
        foreach ($where as $key => &$val) {
148
            if (is_string($key)) {
149
                $where[] = [false === strpos($key, '.') ? $relation . '.' . $key : $key, '=', $val];
150
                unset($where[$key]);
151
            } elseif (isset($val[0]) && false === strpos($val[0], '.')) {
152
                $val[0] = $relation . '.' . $val[0];
153
            }
154
        }
155
    }
156
157
    /**
158
     * 删除记录
159
     * @access public
160
     * @param  mixed $data 表达式 true 表示强制删除
161
     * @return int
162
     * @throws Exception
163
     * @throws PDOException
164
     */
165
    public function delete($data = null): int
166
    {
167
        return $this->query->delete($data);
168
    }
169
170
    /**
171
     * 执行基础查询(仅执行一次)
172
     * @access protected
173
     * @return void
174
     */
175
    protected function baseQuery(): void
176
    {}
0 ignored issues
show
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
177
178
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
179
    {
180
        if ($this->query) {
181
            // 执行基础查询
182
            $this->baseQuery();
183
184
            $result = call_user_func_array([$this->query->getModel(), $method], $args);
185
            $class  = get_class($this->query);
186
187
            return $result instanceof $class ? $this : $result;
188
        }
189
190
        throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
191
    }
192
}
193