-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
175 lines (152 loc) · 4.78 KB
/
Copy pathModel.php
File metadata and controls
175 lines (152 loc) · 4.78 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
require_once __DIR__ . '/BuilderContainer.php';
abstract class Model{
public $pk;
public static function getConnection(){
require_once __DIR__ . '/Connection.php';
return connect();
}
public static function all(){
$columns = self::getArgsList();
array_unshift($columns, self::getPkField());
$builder = new BuilderContainer(get_called_class(), self::getTableName());
return $builder->select()->setColumns($columns);
}
public static function get($query){
$con = self::getConnection();
$list = array();
$result = $con->query($query);
if(!$result){
throw new Exception("Error executing query " . $query, 1);
}
// $description = self::getTableDescription();
while($row = $result->fetch_array(MYSQLI_NUM)){
// for ($i=0; $i < count($row); $i++) { //Convert to correct data type
// $end = strpos($description[$i], '(');
// if(empty($end)){
// $end = strlen($description[$i]);
// }
// switch (substr($description[$i], 0, $end)) {
// case 'int':
// $row[$i] = (int) $row[$i];
// break;
// case 'tinyint':
// $row[$i] = (bool) $row[$i];
// break;
// case 'timestamp':
// $row[$i] = new DateTime($row[$i]);
// break;
// default:
//
// break;
// }
// }
$obj = new static(...array_slice($row, 1, count($row)-1));
$obj->pk = $row[0];
$list[] = $obj;
}
return $list;
}
public static function getTableDescription(){
$con = self::getConnection();
$columns = self::getArgsList();
array_unshift($columns, self::getPkField());
$types = array();
$query = "DESCRIBE " . self::getTableName();
$result = $con->query($query);
while($row = $result->fetch_array()){
if (in_array($row['Field'], $columns)) {
// $types[$row['Field']] = $row['Type'];
array_push($types, $row['Type']);
}
}
if(empty($types)){
throw new Exception("Error getting table description for: " . self::getTableName(), 1);
}
return $types;
}
public static function getByColumnEqual($colName, $colValue){
$columns = self::getArgsList();
array_unshift($columns, self::getPkField());
$builder = new BuilderContainer(get_called_class(), self::getTableName());
$builder->select()->setColumns($columns)->equals($colName, $colValue);
return self::get($builder->getQueryString());
}
public static function getByPkEqual($colValue){
$result = self::getByColumnEqual(self::getPkField(), $colValue);
if(!empty($result)){
return $result[0];
}
return null;
}
public function serialize(){
$array = array();
foreach (self::getArgsList() as $column) {
if(!property_exists(self::class, $column)){
if(gettype($this->{$column}) == 'array'){
$array[$column] = json_encode($this->{$column});
}else{
$array[$column] = $this->{$column};
}
}else{
echo "Column name [" . $column . "] is in constructor but value isn't accessible";
}
}
return $array;
}
public function save(){
$builder = new BuilderContainer(get_called_class(), self::getTableName());
$con = self::getConnection();
if(!empty($this->pk)){ //This record needs to be updated
$query = $builder->update()->setValues($this->serialize())->equals(self::getPkField(), $this->pk);
$query = $query->getQueryString();
if($result = $con->query($query)){
return true;
}else{
throw new Exception("Error executing query: " . $query, 1);
}
}else{ //This record needs to be inserted
$query = $builder->insert()->setValues($this->serialize());
$query = $query->getQueryString();
if($result = $con->query($query)){
$this->{self::getPkField()} = $con->insert_id;
$this->pk = $con->insert_id;
return true;
}else{
throw new Exception("Error executing query: " . $query, 1);
}
}
}
public function delete(){
$builder = new BuilderContainer(get_called_class(), self::getTableName());
$con = self::getConnection();
$query = $builder->delete()->equals(self::getPkField(), $this->pk);
$query = $query->getQueryString();
if($result = $con->query($query)){
return true;
}else{
throw new Exception("Error executing query: " . $query, 1);
}
}
private static function getArgsList($method = '__construct'){
$list = array();
$r = new ReflectionMethod(get_called_class(), $method);
$params = $r->getParameters();
foreach ($params as $param) {
array_push($list, $param->getName());
}
return $list;
}
private static function getTableName(){
if(defined(get_called_class() . '::tableName')){
return static::tableName;
}
return strtolower(static::class);
}
private static function getPkField(){
if(defined(get_called_class() . '::pkField')){
return static::pkField;
}
return 'id';
}
}