| Total Complexity | 43 |
| Total Lines | 337 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like HasMany often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HasMany, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class HasMany extends Relation |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * 架构函数 |
||
| 23 | * @access public |
||
| 24 | * @param Model $parent 上级模型对象 |
||
| 25 | * @param string $model 模型名 |
||
| 26 | * @param string $foreignKey 关联外键 |
||
| 27 | * @param string $localKey 当前模型主键 |
||
| 28 | */ |
||
| 29 | public function __construct(Model $parent, $model, $foreignKey, $localKey) |
||
| 30 | { |
||
| 31 | $this->parent = $parent; |
||
| 32 | $this->model = $model; |
||
|
|
|||
| 33 | $this->foreignKey = $foreignKey; |
||
| 34 | $this->localKey = $localKey; |
||
| 35 | $this->query = (new $model)->db(); |
||
| 36 | |||
| 37 | if (get_class($parent) == $model) { |
||
| 38 | $this->selfRelation = true; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * 延迟获取关联数据 |
||
| 44 | * @access public |
||
| 45 | * @param string $subRelation 子关联名 |
||
| 46 | * @param \Closure $closure 闭包查询条件 |
||
| 47 | * @return \think\Collection |
||
| 48 | */ |
||
| 49 | public function getRelation($subRelation = '', $closure = null) |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * 预载入关联查询 |
||
| 71 | * @access public |
||
| 72 | * @param array $resultSet 数据集 |
||
| 73 | * @param string $relation 当前关联名 |
||
| 74 | * @param string $subRelation 子关联名 |
||
| 75 | * @param \Closure $closure 闭包 |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) |
||
| 79 | { |
||
| 80 | $localKey = $this->localKey; |
||
| 81 | $range = []; |
||
| 82 | |||
| 83 | foreach ($resultSet as $result) { |
||
| 84 | // 获取关联外键列表 |
||
| 85 | if (isset($result->$localKey)) { |
||
| 86 | $range[] = $result->$localKey; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | if (!empty($range)) { |
||
| 91 | $where = [ |
||
| 92 | [$this->foreignKey, 'in', $range], |
||
| 93 | ]; |
||
| 94 | $data = $this->eagerlyOneToMany($where, $relation, $subRelation, $closure); |
||
| 95 | |||
| 96 | // 关联属性名 |
||
| 97 | $attr = Loader::parseName($relation); |
||
| 98 | |||
| 99 | // 关联数据封装 |
||
| 100 | foreach ($resultSet as $result) { |
||
| 101 | $pk = $result->$localKey; |
||
| 102 | if (!isset($data[$pk])) { |
||
| 103 | $data[$pk] = []; |
||
| 104 | } |
||
| 105 | |||
| 106 | foreach ($data[$pk] as &$relationModel) { |
||
| 107 | $relationModel->setParent(clone $result); |
||
| 108 | } |
||
| 109 | |||
| 110 | $result->setRelation($attr, $this->resultSetBuild($data[$pk])); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * 预载入关联查询 |
||
| 117 | * @access public |
||
| 118 | * @param Model $result 数据对象 |
||
| 119 | * @param string $relation 当前关联名 |
||
| 120 | * @param string $subRelation 子关联名 |
||
| 121 | * @param \Closure $closure 闭包 |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | public function eagerlyResult(&$result, $relation, $subRelation, $closure) |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * 关联统计 |
||
| 150 | * @access public |
||
| 151 | * @param Model $result 数据对象 |
||
| 152 | * @param \Closure $closure 闭包 |
||
| 153 | * @param string $aggregate 聚合查询方法 |
||
| 154 | * @param string $field 字段 |
||
| 155 | * @param string $name 统计字段别名 |
||
| 156 | * @return integer |
||
| 157 | */ |
||
| 158 | public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * 创建关联统计子查询 |
||
| 180 | * @access public |
||
| 181 | * @param \Closure $closure 闭包 |
||
| 182 | * @param string $aggregate 聚合查询方法 |
||
| 183 | * @param string $field 字段 |
||
| 184 | * @param string $aggregateAlias 聚合字段别名 |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') |
||
| 188 | { |
||
| 189 | if ($closure) { |
||
| 190 | $return = $closure($this->query); |
||
| 191 | |||
| 192 | if ($return && is_string($return)) { |
||
| 193 | $aggregateAlias = $return; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | return $this->query->alias($aggregate . '_table') |
||
| 198 | ->whereExp($aggregate . '_table.' . $this->foreignKey, '=' . $this->parent->getTable() . '.' . $this->localKey) |
||
| 199 | ->fetchSql() |
||
| 200 | ->$aggregate($field); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * 一对多 关联模型预查询 |
||
| 205 | * @access public |
||
| 206 | * @param array $where 关联预查询条件 |
||
| 207 | * @param string $relation 关联名 |
||
| 208 | * @param string $subRelation 子关联 |
||
| 209 | * @param \Closure $closure |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | protected function eagerlyOneToMany($where, $relation, $subRelation = '', $closure = null) |
||
| 213 | { |
||
| 214 | $foreignKey = $this->foreignKey; |
||
| 215 | |||
| 216 | $this->query->removeWhereField($this->foreignKey); |
||
| 217 | |||
| 218 | // 预载入关联查询 支持嵌套预载入 |
||
| 219 | if ($closure) { |
||
| 220 | $closure($this->query); |
||
| 221 | } |
||
| 222 | |||
| 223 | $list = $this->query->where($where)->with($subRelation)->select(); |
||
| 224 | |||
| 225 | // 组装模型数据 |
||
| 226 | $data = []; |
||
| 227 | |||
| 228 | foreach ($list as $set) { |
||
| 229 | $data[$set->$foreignKey][] = $set; |
||
| 230 | } |
||
| 231 | |||
| 232 | return $data; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * 保存(新增)当前关联数据对象 |
||
| 237 | * @access public |
||
| 238 | * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键 |
||
| 239 | * @param boolean $replace 是否自动识别更新和写入 |
||
| 240 | * @return Model|false |
||
| 241 | */ |
||
| 242 | public function save($data, $replace = true) |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * 创建关联对象实例 |
||
| 251 | * @param array $data |
||
| 252 | * @return Model |
||
| 253 | */ |
||
| 254 | public function make($data = []) |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * 批量保存当前关联数据对象 |
||
| 268 | * @access public |
||
| 269 | * @param array|\think\Collection $dataSet 数据集 |
||
| 270 | * @param boolean $replace 是否自动识别更新和写入 |
||
| 271 | * @return array|false |
||
| 272 | */ |
||
| 273 | public function saveAll($dataSet, $replace = true) |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * 根据关联条件查询当前模型 |
||
| 286 | * @access public |
||
| 287 | * @param string $operator 比较操作符 |
||
| 288 | * @param integer $count 个数 |
||
| 289 | * @param string $id 关联表的统计字段 |
||
| 290 | * @param string $joinType JOIN类型 |
||
| 291 | * @return Query |
||
| 292 | */ |
||
| 293 | public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER') |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * 根据关联条件查询当前模型 |
||
| 313 | * @access public |
||
| 314 | * @param mixed $where 查询条件(数组或者闭包) |
||
| 315 | * @param mixed $fields 字段 |
||
| 316 | * @return Query |
||
| 317 | */ |
||
| 318 | public function hasWhere($where = [], $fields = null) |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * 执行基础查询(仅执行一次) |
||
| 344 | * @access protected |
||
| 345 | * @return void |
||
| 346 | */ |
||
| 347 | protected function baseQuery() |
||
| 360 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..