|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimplePHP\Model; |
|
4
|
|
|
|
|
5
|
|
|
use PDOException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait CRUD to SimplePHP |
|
9
|
|
|
* @package NicollasSilva\SimplePHP |
|
10
|
|
|
*/ |
|
11
|
|
|
trait CRUD { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param int $primary |
|
15
|
|
|
* @return bool |
|
16
|
|
|
*/ |
|
17
|
|
|
public function delete(int $primary): ?bool |
|
18
|
|
|
{ |
|
19
|
|
|
try { |
|
20
|
|
|
$sql = $this->conn->prepare("DELETE FROM {$this->table} WHERE {$this->primary} = :primary"); |
|
21
|
|
|
$sql->bindParam(':primary', $primary); |
|
22
|
|
|
return $sql->execute(); |
|
23
|
|
|
} catch (PDOException $exception) { |
|
24
|
|
|
return null; |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $params |
|
30
|
|
|
* @param array $values |
|
31
|
|
|
* @param int $primary |
|
32
|
|
|
* @return PDOException|bool |
|
33
|
|
|
*/ |
|
34
|
|
|
public function update(String $params, Array $values, Int $primary) |
|
35
|
|
|
{ |
|
36
|
|
|
try { |
|
37
|
|
|
$params = explode(',', $params); |
|
38
|
|
|
$data = []; |
|
39
|
|
|
$countParams = count($params); |
|
40
|
|
|
for ($i = 0; $i < $countParams; $i++) { |
|
41
|
|
|
$data[$i] = ":" . $params[$i] . $i . ", "; |
|
42
|
|
|
} |
|
43
|
|
|
$result = ''; |
|
44
|
|
|
$final = array_map(null, $params, $data); |
|
45
|
|
|
foreach ($final as $key => $vals) { |
|
46
|
|
|
foreach ($vals as $chave => $val) { |
|
47
|
|
|
$result .= str_replace(':', ' = :', $val); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
$result = rtrim($result, ', '); |
|
51
|
|
|
$sql = $this->conn->prepare("UPDATE {$this->table} SET {$result} WHERE {$this->primary} = '{$primary}'"); |
|
52
|
|
|
for ($i = 0; $i < $countParams; $i++) { |
|
53
|
|
|
$data[$i] = ":" . $params[$i] . $i; |
|
54
|
|
|
} |
|
55
|
|
|
$countData = count($data); |
|
56
|
|
|
for ($i = 0; $i < $countData; $i++) { |
|
57
|
|
|
$sql->bindParam($data[$i], $values[$i]); |
|
58
|
|
|
} |
|
59
|
|
|
return $sql->execute(); |
|
60
|
|
|
} catch(PDOException $exception) { |
|
61
|
|
|
echo $exception->getMessage(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $params |
|
67
|
|
|
* @param array $values |
|
68
|
|
|
* @return PDOException|bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function insert(String $params, Array $values) |
|
71
|
|
|
{ |
|
72
|
|
|
try { |
|
73
|
|
|
$parameters = "(".$params.")"; |
|
74
|
|
|
$params = explode(',', $params); |
|
75
|
|
|
$data = []; |
|
76
|
|
|
for($i = 0; $i < count($params); $i++) { |
|
|
|
|
|
|
77
|
|
|
$data[$i] = ":". $params[$i] . $i; |
|
78
|
|
|
} |
|
79
|
|
|
$valueBind = "(".implode(', ', $data).")"; |
|
80
|
|
|
$sql = $this->conn->prepare("INSERT INTO {$this->table} $parameters VALUES $valueBind"); |
|
81
|
|
|
for($i = 0; $i < count($params); $i++) { |
|
|
|
|
|
|
82
|
|
|
$sql->bindParam($data[$i], $values[$i]); |
|
83
|
|
|
} |
|
84
|
|
|
return $sql->execute(); |
|
85
|
|
|
} catch(PDOException $exception) { |
|
86
|
|
|
echo $exception->getCode(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: