|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pyjac\ORM; |
|
4
|
|
|
|
|
5
|
|
|
abstract class Model implements ModelInterface |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
protected $properties = []; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Store instance of database connection used. |
|
13
|
|
|
* @var [type] |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $databaseConnection; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->databaseConnection = DatabaseConnection::getInstance()->databaseConnection; |
|
20
|
|
|
//$databaseConnection->databaseConnection->connect(); |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $key rep column name |
|
24
|
|
|
* @param string $val rep column value |
|
25
|
|
|
* sets into $propertie the $key => $value pairs |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __set($key, $val) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->properties[$key] = $val; |
|
30
|
|
|
} |
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $key reps the column name |
|
33
|
|
|
* @return $key and $value |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __get($key) |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->properties[$key]; |
|
38
|
|
|
} |
|
39
|
|
|
/** |
|
40
|
|
|
* Get all the model properties |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getProperties() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->properties; |
|
47
|
|
|
} |
|
48
|
|
|
/** |
|
49
|
|
|
* Gets the name of the child class only |
|
50
|
|
|
* without the namespace |
|
51
|
|
|
* @var $className |
|
52
|
|
|
* @var $table |
|
53
|
|
|
* @return $table |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getTableName() |
|
56
|
|
|
{ |
|
57
|
|
|
$className = explode('\\', get_called_class()); |
|
58
|
|
|
$table = strtolower(end($className) .'s'); |
|
59
|
|
|
return $table; |
|
60
|
|
|
} |
|
61
|
|
|
/** |
|
62
|
|
|
* returns a particular record |
|
63
|
|
|
* @param $id reps the record id |
|
64
|
|
|
* @param $connection initialised to null |
|
65
|
|
|
* @return object |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function find($id) |
|
68
|
|
|
{ |
|
69
|
|
|
$model = new static; |
|
70
|
|
|
return $model->get($id); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* returns a particular record |
|
75
|
|
|
* @param $id reps the record id |
|
76
|
|
|
* @param $connection initialised to null |
|
77
|
|
|
* @return object |
|
78
|
|
|
*/ |
|
79
|
|
|
public function get($id) |
|
80
|
|
|
{ |
|
81
|
|
|
$sql = "SELECT * FROM {$this->getTableName()} WHERE id={$id}"; |
|
82
|
|
|
$sqlStatement = $this->databaseConnection->prepare($sql); |
|
83
|
|
|
$sqlStatement->setFetchMode($this->databaseConnection::FETCH_CLASS, get_called_class()); |
|
84
|
|
|
$sqlStatement->execute(); |
|
85
|
|
|
if($sqlStatement->rowCount() < 1){ |
|
86
|
|
|
throw new ModelNotFoundException($id); |
|
87
|
|
|
} |
|
88
|
|
|
return $sqlStatement->fetch(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public static function getAll() |
|
92
|
|
|
{ |
|
93
|
|
|
$model = new static; |
|
94
|
|
|
return $model->all(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function all() |
|
98
|
|
|
{ |
|
99
|
|
|
$sql = "SELECT * FROM {$this->getTableName()}"; |
|
100
|
|
|
$row = $this->databaseConnection->prepare($sql); |
|
101
|
|
|
$row->execute(); |
|
102
|
|
|
|
|
103
|
|
|
return $row->fetchAll($this->databaseConnection::FETCH_CLASS); |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
/** |
|
107
|
|
|
* Update the model in the database. |
|
108
|
|
|
* |
|
109
|
|
|
* @return int |
|
110
|
|
|
*/ |
|
111
|
|
|
private function update() |
|
112
|
|
|
{ |
|
113
|
|
|
|
|
114
|
|
|
$columnNames = ""; |
|
115
|
|
|
$columnValues = ""; |
|
116
|
|
|
$bindNameParameters = []; |
|
117
|
|
|
$sqlUpdate = "UPDATE " . $this->getTableName() . " SET " ; |
|
118
|
|
|
foreach ($this->properties as $columnName => $columnValue) { |
|
119
|
|
|
if($key == 'id') continue; |
|
120
|
|
|
$bindColumnName = ':' . $columnName; |
|
121
|
|
|
$sqlUpdate .= "$columnName = $bindColumnName,"; |
|
122
|
|
|
$bindNameParameters[$bindColumnName] = $columnValue |
|
123
|
|
|
} |
|
|
|
|
|
|
124
|
|
|
//Remove the last comma in sql command then join it to the other query part. |
|
125
|
|
|
$sqlUpdate = substr($sqlUpdate, 0, -1)." WHERE id = :id"; |
|
126
|
|
|
$sqlStatement = $this->databaseConnection->prepare($sqlUpdate); |
|
127
|
|
|
$sqlStatement->bindValue(":id", $this->properties['id']); |
|
128
|
|
|
$sqlStatement->execute($bindNameParameters); |
|
129
|
|
|
return $sqlStatement->rowCount(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Insert the model values into the database. |
|
134
|
|
|
* |
|
135
|
|
|
* @return int |
|
136
|
|
|
*/ |
|
137
|
|
|
private function create() |
|
138
|
|
|
{ |
|
139
|
|
|
|
|
140
|
|
|
$columnNames = ""; |
|
141
|
|
|
$columnValues = ""; |
|
142
|
|
|
$bindNameParameters = []; |
|
143
|
|
|
$sqlCreate = "INSERT" . " INTO " . $this->getTableName()." ("; |
|
144
|
|
|
foreach ($this->properties as $columnName => $columnValue) { |
|
145
|
|
|
|
|
146
|
|
|
$bindColumnName = ':' . $columnName; |
|
147
|
|
|
$columnNames .= $columnName.","; |
|
148
|
|
|
$columnValues .= $bindColumnName.","; |
|
149
|
|
|
$bindNameParameters[$bindColumnName] = $columnValue |
|
150
|
|
|
} |
|
151
|
|
|
// Remove ending comma and whitespace. |
|
152
|
|
|
$columnNames = substr($columnNames, 0, -1); |
|
153
|
|
|
$columnValues = substr($columnValues, 0, -1); |
|
154
|
|
|
|
|
155
|
|
|
$sqlCreate .= $columnNames.') VALUES (' .$columnValues.')'; |
|
156
|
|
|
$sqlStatement = $this->databaseConnection->prepare($sqlCreate); |
|
157
|
|
|
$sqlStatement->execute($bindNameParameters); |
|
158
|
|
|
return $sqlStatement->rowCount(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Save the model data to the database. |
|
163
|
|
|
* |
|
164
|
|
|
* @return boolean |
|
165
|
|
|
*/ |
|
166
|
|
|
public function save() |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->id ? $this->update() : $this->create(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Delete a model from the database. |
|
173
|
|
|
* @param int $id |
|
174
|
|
|
* @return boolean |
|
175
|
|
|
*/ |
|
176
|
|
|
public static function destroy($id) |
|
177
|
|
|
{ |
|
178
|
|
|
$model = new static; |
|
179
|
|
|
return $model->delete($id); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Delete model from the database. |
|
184
|
|
|
* |
|
185
|
|
|
* @param int $id |
|
186
|
|
|
* @return boolean |
|
187
|
|
|
*/ |
|
188
|
|
|
public function delete($id) |
|
189
|
|
|
{ |
|
190
|
|
|
$sql = "DELETE" . " FROM " . self::getTableName()." WHERE id = ". $id; |
|
191
|
|
|
$sqlStatment = $this->databaseConnection->prepare($sql); |
|
192
|
|
|
$sqlStatment->execute(); |
|
193
|
|
|
return ($sqlStatment->rowCount() > 0) ? true : false; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.