Completed
Push — 6.0 ( be0b6e...c47dd5 )
by liu
06:54 queued 10s
created

UpdateCheck::parsePkWhere()   B

Complexity

Conditions 7
Paths 33

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 33
nop 1
dl 0
loc 23
ccs 0
cts 13
cp 0
crap 56
rs 8.8333
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
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\db\concern;
14
15
use think\db\Exception;
16
17
trait UpdateCheck
18
{
19
    /**
20
     * 分析数据是否存在更新条件
21
     * @access public
22
     * @param array $data 数据
23
     * @return bool
24
     * @throws Exception
25
     */
26
    public function parseUpdateData(&$data): bool
27
    {
28
        $pk       = $this->getPk();
0 ignored issues
show
Bug introduced by
It seems like getPk() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

28
        /** @scrutinizer ignore-call */ 
29
        $pk       = $this->getPk();
Loading history...
29
        $isUpdate = false;
30
        // 如果存在主键数据 则自动作为更新条件
31
        if (is_string($pk) && isset($data[$pk])) {
32
            $this->where($pk, '=', $data[$pk]);
0 ignored issues
show
Bug introduced by
It seems like where() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

32
            $this->/** @scrutinizer ignore-call */ 
33
                   where($pk, '=', $data[$pk]);
Loading history...
33
            $this->options['key'] = $data[$pk];
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
            unset($data[$pk]);
35
            $isUpdate = true;
36
        } elseif (is_array($pk)) {
37
            foreach ($pk as $field) {
38
                if (isset($data[$field])) {
39
                    $this->where($field, '=', $data[$field]);
40
                    $isUpdate = true;
41
                } else {
42
                    // 如果缺少复合主键数据则不执行
43
                    throw new Exception('miss complex primary data');
44
                }
45
                unset($data[$field]);
46
            }
47
        }
48
49
        return $isUpdate;
50
    }
51
52
    /**
53
     * 把主键值转换为查询条件 支持复合主键
54
     * @access public
55
     * @param array|string $data 主键数据
56
     * @return void
57
     * @throws Exception
58
     */
59
    public function parsePkWhere($data): void
60
    {
61
        $pk = $this->getPk();
62
63
        if (is_string($pk)) {
64
            // 获取数据表
65
            if (empty($this->options['table'])) {
66
                $this->options['table'] = $this->getTable();
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

66
                /** @scrutinizer ignore-call */ 
67
                $this->options['table'] = $this->getTable();
Loading history...
67
            }
68
69
            $table = is_array($this->options['table']) ? key($this->options['table']) : $this->options['table'];
70
71
            if (!empty($this->options['alias'][$table])) {
72
                $alias = $this->options['alias'][$table];
73
            }
74
75
            $key = isset($alias) ? $alias . '.' . $pk : $pk;
76
            // 根据主键查询
77
            if (is_array($data)) {
78
                $this->where($key, 'in', $data);
79
            } else {
80
                $this->where($key, '=', $data);
81
                $this->options['key'] = $data;
82
            }
83
        }
84
    }
85
86
    /**
87
     * 获取模型的更新条件
88
     * @access protected
89
     * @param array $options 查询参数
90
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
91
    protected function getModelUpdateCondition(array $options)
92
    {
93
        return $options['where']['AND'] ?? null;
94
    }
95
}
96