-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMainModel.php
124 lines (106 loc) · 3.14 KB
/
MainModel.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace App\Model;
/**
* Class MainModel
* Creates Queries for CRUD
* @package App\Model
*/
abstract class MainModel
{
/**
* Database
* @var PdoDb
*/
protected $database = null;
/**
* Database Table
* @var string
*/
protected $table = null;
/**
* Model constructor
* Receives the Database Object & creates the Table Name
* @param PdoDb $database
*/
public function __construct(PdoDb $database)
{
$this->database = $database;
$model = explode("\\", get_class($this));
$this->table = ucfirst(str_replace("Model", "", array_pop($model)));
}
/**
* Lists all Datas from the id or another key
* @param string $value
* @param string $key
* @return array|mixed
*/
public function listData(string $value = null, string $key = null)
{
if (isset($key)) {
$query = "SELECT * FROM " . $this->table . " WHERE " . $key . " = ?";
return $this->database->getAllData($query, [$value]);
}
$query = "SELECT * FROM " . $this->table;
return $this->database->getAllData($query);
}
/**
* Creates a new Data entry
* @param array $data
*/
public function createData(array $data)
{
$keys = implode(", ", array_keys($data));
$values = implode("', '", $data);
$query = "INSERT INTO " . $this->table . " (" . $keys . ") VALUES ('" . $values . "')";
$this->database->setData($query);
}
/**
* Reads Data from its id or another key
* @param string $value
* @param string|null $key
* @return mixed
*/
public function readData(string $value, string $key = null)
{
if (isset($key)) {
$query = "SELECT * FROM " . $this->table . " WHERE " . $key . " = ?";
} else {
$query = "SELECT * FROM " . $this->table . " WHERE id = ?";
}
return $this->database->getData($query, [$value]);
}
/**
* Updates Data from its id or another key
* @param string $value
* @param array $data
* @param string|null $key
*/
public function updateData(string $value, array $data, string $key = null)
{
$set = null;
foreach ($data as $dataKey => $dataValue) {
$set .= $dataKey . " = '" . $dataValue . "', ";
}
$set = substr_replace($set, "", -2);
if (isset($key)) {
$query = "UPDATE " . $this->table . " SET " . $set . " WHERE " . $key . " = ?";
} else {
$query = "UPDATE " . $this->table . " SET " . $set . " WHERE id = ?";
}
$this->database->setData($query, [$value]);
}
/**
* Deletes Data from its id or another key
* @param string $value
* @param string|null $key
*/
public function deleteData(string $value, string $key = null)
{
if (isset($key)) {
$query = "DELETE FROM " . $this->table . " WHERE " . $key . " = ?";
} else {
$query = "DELETE FROM " . $this->table . " WHERE id = ?";
}
$this->database->setData($query, [$value]);
}
}