1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the eav package. |
4
|
|
|
* @author Aleksandr Drobotik <[email protected]> |
5
|
|
|
* @copyright 2023 Aleksandr Drobotik |
6
|
|
|
* @license https://opensource.org/license/mit The MIT License |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Drobotik\Eav\Model; |
11
|
|
|
|
12
|
|
|
use Drobotik\Eav\Database\Connection; |
13
|
|
|
use Doctrine\DBAL\Connection as DBALConnection; |
14
|
|
|
use PDO; |
15
|
|
|
|
16
|
|
|
class Model |
17
|
|
|
{ |
18
|
|
|
protected string $primaryKey = 'id'; |
19
|
|
|
|
20
|
|
|
protected string $table; |
21
|
|
|
|
22
|
1 |
|
public function db() : DBALConnection |
23
|
|
|
{ |
24
|
1 |
|
return Connection::get(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getNativeConnection() : PDO |
28
|
|
|
{ |
29
|
|
|
return Connection::getNativeConnection(); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
public function setPrimaryKey(string $name): void |
33
|
|
|
{ |
34
|
1 |
|
$this->primaryKey = $name; |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
public function getPrimaryKey(): string |
38
|
|
|
{ |
39
|
1 |
|
return $this->primaryKey; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function setTable(string $table): void |
43
|
|
|
{ |
44
|
1 |
|
$this->table = $table; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function getTable() : string |
48
|
|
|
{ |
49
|
1 |
|
return $this->table; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function insert(array $data): int |
53
|
|
|
{ |
54
|
1 |
|
$conn = $this->db()->getNativeConnection(); |
55
|
1 |
|
$table = $this->getTable(); |
56
|
|
|
|
57
|
|
|
// Extract the column names and values from the data array |
58
|
1 |
|
$columns = implode(', ', array_keys($data)); |
59
|
1 |
|
$placeholders = ':' . implode(', :', array_keys($data)); |
60
|
|
|
|
61
|
|
|
// Prepare the SQL statement |
62
|
1 |
|
$stmt = $conn->prepare("INSERT INTO $table ($columns) VALUES ($placeholders)"); |
63
|
|
|
// Bind the values from the $data array to the prepared statement |
64
|
1 |
|
foreach ($data as $key => $value) { |
65
|
1 |
|
$stmt->bindValue(':' . $key, $value); |
66
|
|
|
} |
67
|
1 |
|
$stmt->execute(); |
68
|
|
|
|
69
|
1 |
|
return (int) $conn->lastInsertId(); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function updateByArray(int $key, array $data) : bool |
73
|
|
|
{ |
74
|
1 |
|
$keyName = $this->getPrimaryKey(); |
75
|
|
|
|
76
|
1 |
|
$conn = $this->db()->getNativeConnection(); |
77
|
1 |
|
$table = $this->getTable(); |
78
|
|
|
|
79
|
|
|
// Prepare the update statement |
80
|
1 |
|
$columns = array_keys($data); |
81
|
1 |
|
$setValues = implode(' = ?, ', $columns) . ' = ?'; |
82
|
|
|
|
83
|
1 |
|
$stmt = $conn->prepare("UPDATE $table SET $setValues WHERE $keyName = :key"); |
84
|
|
|
|
85
|
|
|
// Bind the data parameters |
86
|
1 |
|
$index = 1; |
87
|
1 |
|
foreach ($data as $value) { |
88
|
1 |
|
$stmt->bindValue($index, $value); |
89
|
1 |
|
$index++; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Bind the ID parameter |
93
|
1 |
|
$stmt->bindValue(':key', $key, PDO::PARAM_INT); |
94
|
1 |
|
return $stmt->execute(); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function deleteByKey(int $key): bool |
98
|
|
|
{ |
99
|
1 |
|
$keyName = $this->getPrimaryKey(); |
100
|
|
|
|
101
|
1 |
|
$conn = $this->db()->getNativeConnection(); |
102
|
1 |
|
$table = $this->getTable(); |
103
|
|
|
|
104
|
1 |
|
$stmt = $conn->prepare("DELETE FROM $table WHERE $keyName = :key"); |
105
|
1 |
|
$stmt->bindValue(':key', $key, PDO::PARAM_INT); |
106
|
|
|
|
107
|
1 |
|
return $stmt->execute(); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
public function count() : int |
111
|
|
|
{ |
112
|
1 |
|
$conn = $this->db()->getNativeConnection(); |
113
|
1 |
|
$table = $this->getTable(); |
114
|
1 |
|
$stmt = $conn->prepare("SELECT COUNT(*) as count FROM $table"); |
115
|
1 |
|
$stmt->execute(); |
116
|
1 |
|
$record = $stmt->fetch(PDO::FETCH_ASSOC); |
117
|
1 |
|
return (int) $record["count"]; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
public function findByKey(int $key) : array|false |
121
|
|
|
{ |
122
|
1 |
|
return $this->db() |
123
|
1 |
|
->createQueryBuilder() |
124
|
1 |
|
->select('*') |
125
|
1 |
|
->from($this->getTable()) |
126
|
1 |
|
->where(sprintf('%s = ?', $this->getPrimaryKey())) |
127
|
1 |
|
->setParameter(0, $key, PDO::PARAM_INT) |
128
|
1 |
|
->executeQuery() |
129
|
1 |
|
->fetchAssociative(); |
130
|
|
|
} |
131
|
|
|
} |