Passed
Push — 5.1 ( 504e06...b5c159 )
by liu
09:18 queued 01:53
created

Relation::baseQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 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~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  mixed $data 表达式 true 表示强制删除
146
     * @return int
147
     * @throws Exception
148
     * @throws PDOException
149
     */
150
    public function delete($data = null)
151
    {
152
        return $this->query->delete($data);
153
    }
154
155
    /**
156
     * 执行基础查询(仅执行一次)
157
     * @access protected
158
     * @return void
159
     */
160
    protected function baseQuery()
161
    {}
0 ignored issues
show
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
162
163
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
164
    {
165
        if ($this->query) {
166
            // 执行基础查询
167
            $this->baseQuery();
168
169
            $result = call_user_func_array([$this->query->getModel(), $method], $args);
170
171
            return $result === $this->query && !in_array(strtolower($method), ['fetchsql', 'fetchpdo']) ? $this : $result;
172
        } else {
173
            throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
174
        }
175
    }
176
}
177