Passed
Push — 5.1 ( 5a1c5d...1919c6 )
by liu
08:28
created

Relation   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 161
rs 10
c 0
b 0
f 0
wmc 25

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 3 1
A getParent() 0 3 1
A getRelationQueryFields() 0 18 5
A getQuery() 0 3 1
A getQueryFields() 0 4 1
A selfRelation() 0 4 1
A getQueryWhere() 0 8 6
A isSelfRelation() 0 3 1
A resultSetBuild() 0 3 1
A baseQuery() 0 2 1
A update() 0 3 1
A __call() 0 11 4
A delete() 0 3 1
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~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
0 ignored issues
show
Coding Style introduced by
Package name "think\model" is not valid; consider "Thinkmodel" instead
Loading history...
21
 *
22
 * @mixin Query
23
 */
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...
24
abstract class Relation
25
{
26
    // 父模型对象
27
    protected $parent;
28
    /** @var  Model 当前关联的模型类 */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
29
    protected $model;
30
    /** @var Query 关联模型查询对象 */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
105
    {
106
        $fields = $this->query->getOptions('field');
107
        return $this->getRelationQueryFields($fields, $model);
108
    }
109
110
    protected function getRelationQueryFields($fields, $model)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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
    {}
0 ignored issues
show
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
173
174
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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