-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataBaseGateway.php
44 lines (38 loc) · 1.09 KB
/
DataBaseGateway.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
class DataBaseGateway
{
protected PDO $pdo;
public function __construct($pdo)
{
$this->pdo = $pdo;
}
public function add($table, $data)
{
$keys = implode(',', array_keys($data));
$tags = ":" . implode(',:', array_keys($data));
$sql = "INSERT INTO {$table} ({$keys}) VALUE ({$tags})";
$statement = $this->pdo->prepare($sql);
$statement->execute($data);
}
public function update($table, $data, $id)
{
$keys = array_keys($data);
$str = '';
foreach ($keys as $key) {
$str .= $key . '=:' . $key . ',';
}
$keys = rtrim($str, ',');
$data['id'] = $id;
$sql = "UPDATE {$table} SET {$keys} WHERE id=:id";
$statement = $this->pdo->prepare($sql);
$statement->bindValue(':id', $id);
$statement->execute($data);
}
public function delete($table, $id)
{
$sql = "DELETE FROM {$table} WHERE id=:id";
$statement = $this->pdo->prepare($sql);
$statement->bindParam(':id', $id);
$statement->execute();
}
}