1 | <?php |
||||
2 | |||||
3 | namespace SimplePHP\Model; |
||||
4 | |||||
5 | use PDOException; |
||||
6 | use SimplePHP\Root\Connection; |
||||
7 | |||||
8 | /** |
||||
9 | * Trait CRUD to SimplePHP |
||||
10 | * @package NicollasSilva\SimplePHP |
||||
11 | */ |
||||
12 | trait CRUD |
||||
13 | { |
||||
14 | |||||
15 | /** |
||||
16 | * @param int $primary |
||||
17 | * @return bool|null |
||||
18 | */ |
||||
19 | public function delete(int $primary): ?bool |
||||
20 | { |
||||
21 | try { |
||||
22 | $sql = Connection::getConnection()->prepare("DELETE FROM {$this->table} WHERE {$this->primary} = :primary"); |
||||
23 | $sql->bindParam(':primary', $primary); |
||||
24 | return $sql->execute(); |
||||
25 | } catch (PDOException $exception) { |
||||
26 | return $this->writeLog($exception->getMessage(), true); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
27 | } |
||||
28 | } |
||||
29 | |||||
30 | /** |
||||
31 | * @param string $params |
||||
32 | * @param array $values |
||||
33 | * @param int $primary |
||||
34 | * @return bool|null |
||||
35 | */ |
||||
36 | public function update(String $params, array $values, Int $primary) |
||||
37 | { |
||||
38 | try { |
||||
39 | $params = explode(',', $params); |
||||
40 | $data = []; |
||||
41 | $countParams = count($params); |
||||
42 | for ($i = 0; $i < $countParams; $i++) { |
||||
43 | $data[$i] = ":" . $params[$i] . $i . ", "; |
||||
44 | } |
||||
45 | $result = ''; |
||||
46 | $final = array_map(null, $params, $data); |
||||
0 ignored issues
–
show
null of type null is incompatible with the type callable expected by parameter $callback of array_map() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
47 | foreach ($final as $key => $vals) { |
||||
48 | foreach ($vals as $chave => $val) { |
||||
49 | $result .= str_replace(':', ' = :', $val); |
||||
50 | } |
||||
51 | } |
||||
52 | $result = rtrim($result, ', '); |
||||
53 | $sql = Connection::getConnection()->prepare("UPDATE {$this->table} SET {$result} WHERE {$this->primary} = '{$primary}'"); |
||||
54 | for ($i = 0; $i < $countParams; $i++) { |
||||
55 | $data[$i] = ":" . $params[$i] . $i; |
||||
56 | } |
||||
57 | $countData = count($data); |
||||
58 | for ($i = 0; $i < $countData; $i++) { |
||||
59 | $sql->bindParam($data[$i], $values[$i]); |
||||
60 | } |
||||
61 | return $sql->execute(); |
||||
62 | } catch (PDOException $exception) { |
||||
63 | return $this->writeLog($exception->getMessage(), true); |
||||
64 | } |
||||
65 | } |
||||
66 | |||||
67 | /** |
||||
68 | * @param string $params |
||||
69 | * @param array $values |
||||
70 | * @return bool|null |
||||
71 | */ |
||||
72 | public function insert(String $params, array $values) |
||||
73 | { |
||||
74 | try { |
||||
75 | $parameters = "(" . $params . ")"; |
||||
76 | $params = explode(',', $params); |
||||
77 | $data = []; |
||||
78 | $countParams = count($params); |
||||
79 | for ($i = 0; $i < $countParams; $i++) { |
||||
80 | $data[$i] = ":" . $params[$i] . $i; |
||||
81 | } |
||||
82 | $valueBind = "(" . implode(', ', $data) . ")"; |
||||
83 | $sql = Connection::getConnection()->prepare("INSERT INTO {$this->table} $parameters VALUES $valueBind"); |
||||
84 | for ($i = 0; $i < $countParams; $i++) { |
||||
85 | $sql->bindParam($data[$i], $values[$i]); |
||||
86 | } |
||||
87 | return $sql->execute(); |
||||
88 | } catch (PDOException $exception) { |
||||
89 | return $this->writeLog($exception->getMessage(), true); |
||||
90 | } |
||||
91 | } |
||||
92 | } |
||||
93 |