Passed
Push — 6.0 ( ae3266...6faeca )
by liu
04:09
created

HasOneThrough::baseQuery()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 2
nop 0
dl 0
loc 19
ccs 0
cts 16
cp 0
crap 12
rs 9.7666
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
12
namespace think\model\relation;
13
14
use Closure;
15
use think\App;
16
use think\db\Query;
17
use think\Exception;
18
use think\Model;
19
use think\model\Relation;
20
21
/**
22
 * 远程一对一关联类
23
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package 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
class HasOneThrough extends Relation
25
{
26
    /**
27
     * 中间关联表外键
28
     * @var string
29
     */
30
    protected $throughKey;
31
32
    /**
33
     * 中间表模型
34
     * @var string
35
     */
36
    protected $through;
37
38
    /**
39
     * 架构函数
40
     * @access public
41
     * @param  Model  $parent     上级模型对象
42
     * @param  string $model      模型名
43
     * @param  string $through    中间模型名
44
     * @param  string $foreignKey 关联外键
45
     * @param  string $throughKey 关联外键
46
     * @param  string $localKey   当前主键
47
     */
48
    public function __construct(Model $parent, string $model, string $through, string $foreignKey, string $throughKey, string $localKey)
49
    {
50
        $this->parent     = $parent;
51
        $this->model      = $model;
52
        $this->through    = $through;
53
        $this->foreignKey = $foreignKey;
54
        $this->throughKey = $throughKey;
55
        $this->localKey   = $localKey;
56
        $this->query      = (new $model)->db();
57
    }
58
59
    /**
60
     * 延迟获取关联数据
61
     * @access public
62
     * @param  array   $subRelation 子关联名
63
     * @param  Closure $closure     闭包查询条件
64
     * @return Model
65
     */
66
    public function getRelation(array $subRelation = [], \Closure $closure = null)
67
    {
68
        if ($closure) {
69
            $closure($this->query);
70
        }
71
72
        $this->baseQuery();
73
74
        return $this->query->relation($subRelation)->find();
75
    }
76
77
    /**
78
     * 根据关联条件查询当前模型
79
     * @access public
80
     * @param  string  $operator 比较操作符
81
     * @param  integer $count    个数
82
     * @param  string  $id       关联表的统计字段
83
     * @param  string  $joinType JOIN类型
84
     * @return Query
85
     */
86
    public function has(string $operator = '>=', int $count = 1, string $id = '*', $joinType = '')
0 ignored issues
show
Unused Code introduced by
The parameter $operator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

86
    public function has(/** @scrutinizer ignore-unused */ string $operator = '>=', int $count = 1, string $id = '*', $joinType = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

86
    public function has(string $operator = '>=', int $count = 1, /** @scrutinizer ignore-unused */ string $id = '*', $joinType = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $count is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

86
    public function has(string $operator = '>=', /** @scrutinizer ignore-unused */ int $count = 1, string $id = '*', $joinType = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $joinType is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

86
    public function has(string $operator = '>=', int $count = 1, string $id = '*', /** @scrutinizer ignore-unused */ $joinType = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
        return $this->parent;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->parent returns the type think\Model which is incompatible with the documented return type think\db\Query.
Loading history...
89
    }
90
91
    /**
92
     * 根据关联条件查询当前模型
93
     * @access public
94
     * @param  mixed  $where 查询条件(数组或者闭包)
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
95
     * @param  mixed  $fields 字段
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
96
     * @param  string $joinType JOIN类型
97
     * @return Query
98
     */
99
    public function hasWhere($where = [], $fields = null, $joinType = '')
0 ignored issues
show
Unused Code introduced by
The parameter $where is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    public function hasWhere(/** @scrutinizer ignore-unused */ $where = [], $fields = null, $joinType = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    public function hasWhere($where = [], /** @scrutinizer ignore-unused */ $fields = null, $joinType = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $joinType is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    public function hasWhere($where = [], $fields = null, /** @scrutinizer ignore-unused */ $joinType = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        throw new Exception('relation not support: hasWhere');
102
    }
103
104
    /**
105
     * 预载入关联查询
106
     * @access public
107
     * @param  array   $resultSet   数据集
108
     * @param  string  $relation    当前关联名
109
     * @param  array   $subRelation 子关联名
110
     * @param  Closure $closure     闭包
111
     * @return void
112
     */
113
    public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation = [], Closure $closure): void
0 ignored issues
show
Unused Code introduced by
The parameter $subRelation is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

113
    public function eagerlyResultSet(array &$resultSet, string $relation, /** @scrutinizer ignore-unused */ array $subRelation = [], Closure $closure): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $closure is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

113
    public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation = [], /** @scrutinizer ignore-unused */ Closure $closure): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $resultSet is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

113
    public function eagerlyResultSet(/** @scrutinizer ignore-unused */ array &$resultSet, string $relation, array $subRelation = [], Closure $closure): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $relation is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

113
    public function eagerlyResultSet(array &$resultSet, /** @scrutinizer ignore-unused */ string $relation, array $subRelation = [], Closure $closure): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
114
    {}
0 ignored issues
show
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
115
116
    /**
117
     * 预载入关联查询 返回模型对象
118
     * @access public
119
     * @param  Model   $result      数据对象
120
     * @param  string  $relation    当前关联名
121
     * @param  array   $subRelation 子关联名
122
     * @param  Closure $closure     闭包
123
     * @return void
124
     */
125
    public function eagerlyResult(Model $result, string $relation, array $subRelation = [], Closure $closure = null): void
0 ignored issues
show
Unused Code introduced by
The parameter $closure is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

125
    public function eagerlyResult(Model $result, string $relation, array $subRelation = [], /** @scrutinizer ignore-unused */ Closure $closure = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $relation is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

125
    public function eagerlyResult(Model $result, /** @scrutinizer ignore-unused */ string $relation, array $subRelation = [], Closure $closure = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $subRelation is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

125
    public function eagerlyResult(Model $result, string $relation, /** @scrutinizer ignore-unused */ array $subRelation = [], Closure $closure = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $result is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

125
    public function eagerlyResult(/** @scrutinizer ignore-unused */ Model $result, string $relation, array $subRelation = [], Closure $closure = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
    {}
0 ignored issues
show
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
127
128
    /**
129
     * 关联统计
130
     * @access public
131
     * @param  Model   $result  数据对象
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 2 found
Loading history...
132
     * @param  Closure $closure 闭包
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
133
     * @param  string  $aggregate 聚合查询方法
134
     * @param  string  $field 字段
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
135
     * @return integer
136
     */
137
    public function relationCount(Model $result, Closure $closure, string $aggregate = 'count', string $field = '*')
0 ignored issues
show
Unused Code introduced by
The parameter $result is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

137
    public function relationCount(/** @scrutinizer ignore-unused */ Model $result, Closure $closure, string $aggregate = 'count', string $field = '*')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

137
    public function relationCount(Model $result, Closure $closure, string $aggregate = 'count', /** @scrutinizer ignore-unused */ string $field = '*')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $aggregate is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

137
    public function relationCount(Model $result, Closure $closure, /** @scrutinizer ignore-unused */ string $aggregate = 'count', string $field = '*')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $closure is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

137
    public function relationCount(Model $result, /** @scrutinizer ignore-unused */ Closure $closure, string $aggregate = 'count', string $field = '*')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
    {}
0 ignored issues
show
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
139
140
    /**
141
     * 执行基础查询(仅执行一次)
142
     * @access protected
143
     * @return void
144
     */
145
    protected function baseQuery(): void
146
    {
147
        if (empty($this->baseQuery) && $this->parent->getData()) {
148
            $through      = $this->through;
149
            $alias        = App::parseName(App::classBaseName($this->model));
150
            $throughTable = $through::getTable();
151
            $pk           = (new $through)->getPk();
152
            $throughKey   = $this->throughKey;
153
            $modelTable   = $this->parent->getTable();
0 ignored issues
show
Bug introduced by
The method getTable() does not exist on think\Model. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
            /** @scrutinizer ignore-call */ 
154
            $modelTable   = $this->parent->getTable();
Loading history...
154
            $fields       = $this->getQueryFields($alias);
155
156
            $this->query
157
                ->field($fields)
158
                ->alias($alias)
159
                ->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey)
160
                ->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey)
161
                ->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey});
162
163
            $this->baseQuery = true;
164
        }
165
    }
166
167
}
168