@@ 141-356 (lines=216) @@ | ||
138 | } |
|
139 | } |
|
140 | ||
141 | abstract class Model implements ModelInterface |
|
142 | { |
|
143 | ||
144 | ||
145 | protected $properties = []; |
|
146 | ||
147 | /** |
|
148 | * Store instance of database connection used. |
|
149 | * @var [type] |
|
150 | */ |
|
151 | protected $databaseConnection; |
|
152 | ||
153 | public function __construct() |
|
154 | { |
|
155 | $this->databaseConnection = DatabaseConnection::getInstance()->databaseConnection; |
|
156 | //$databaseConnection->databaseConnection->connect(); |
|
157 | } |
|
158 | /** |
|
159 | * @param string $key rep column name |
|
160 | * @param string $val rep column value |
|
161 | * sets into $propertie the $key => $value pairs |
|
162 | */ |
|
163 | public function __set($key, $val) |
|
164 | { |
|
165 | $this->properties[$key] = $val; |
|
166 | } |
|
167 | /** |
|
168 | * @param string $key reps the column name |
|
169 | * @return $key and $value |
|
170 | */ |
|
171 | public function __get($key) |
|
172 | { |
|
173 | return $this->properties[$key]; |
|
174 | } |
|
175 | /** |
|
176 | * Get all the model properties |
|
177 | * |
|
178 | * @return array |
|
179 | */ |
|
180 | public function getProperties() |
|
181 | { |
|
182 | return $this->properties; |
|
183 | } |
|
184 | /** |
|
185 | * Gets the name of the child class only |
|
186 | * without the namespace |
|
187 | * @var $className |
|
188 | * @var $table |
|
189 | * @return $table |
|
190 | */ |
|
191 | public function getTableName() |
|
192 | { |
|
193 | $className = explode('\\', get_called_class()); |
|
194 | $table = strtolower(end($className) .'s'); |
|
195 | return $table; |
|
196 | } |
|
197 | /** |
|
198 | * returns a particular record |
|
199 | * @param $id reps the record id |
|
200 | * @param $connection initialised to null |
|
201 | * @return object |
|
202 | */ |
|
203 | public static function find($id) |
|
204 | { |
|
205 | $model = new static; |
|
206 | return $model->get($id); |
|
207 | } |
|
208 | /** |
|
209 | * returns a particular record |
|
210 | * @param $id reps the record id |
|
211 | * @param $connection initialised to null |
|
212 | * @return object |
|
213 | */ |
|
214 | public function get($id) |
|
215 | { |
|
216 | $sql = "SELECT * FROM {$this->getTableName()} WHERE id={$id}"; |
|
217 | $sqlStatement = $this->databaseConnection->prepare($sql); |
|
218 | $sqlStatement->setFetchMode(PDO::FETCH_CLASS, get_called_class()); |
|
219 | $sqlStatement->execute(); |
|
220 | if($sqlStatement->rowCount() < 1){ |
|
221 | throw new ModelNotFoundException($id); |
|
222 | } |
|
223 | return $sqlStatement->fetch(); |
|
224 | } |
|
225 | ||
226 | public static function getAll() |
|
227 | { |
|
228 | $model = new static; |
|
229 | return $model->all(); |
|
230 | } |
|
231 | ||
232 | public function all() |
|
233 | { |
|
234 | $sql = "SELECT * FROM {$this->getTableName()}"; |
|
235 | $row = $this->databaseConnection->prepare($sql); |
|
236 | $row->execute(); |
|
237 | ||
238 | return $row->fetchAll($this->databaseConnection::FETCH_CLASS); |
|
239 | ||
240 | } |
|
241 | /** update table with instance properties |
|
242 | * |
|
243 | */ |
|
244 | private function update() |
|
245 | { |
|
246 | $connection = $this->getConnection(); |
|
247 | $columnNames = ""; |
|
248 | $columnValues = ""; |
|
249 | $count = 0; |
|
250 | $update = "UPDATE " . $this->getTableName() . " SET " ; |
|
251 | foreach ($this->properties as $key => $val) { |
|
252 | $count++; |
|
253 | if(($key == 'id')) continue; |
|
254 | $update .= "$key = '$val'"; |
|
255 | if ($count < count($this->properties) ) |
|
256 | { |
|
257 | $update .=","; |
|
258 | } |
|
259 | } |
|
260 | $update .= " WHERE id = " . $this->properties['id']; |
|
261 | $stmt = $connection->prepare($update); |
|
262 | foreach ($this->properties as $key => $val) { |
|
263 | if($key == 'id') continue; |
|
264 | } |
|
265 | $stmt->execute(); |
|
266 | return $stmt->rowCount(); |
|
267 | } |
|
268 | /** |
|
269 | * insert instance data into the table |
|
270 | */ |
|
271 | private function create() |
|
272 | { |
|
273 | $connection = $this->getConnection(); |
|
274 | $columnNames = ""; |
|
275 | $columnValues = ""; |
|
276 | $count = 0; |
|
277 | $create = "INSERT" . " INTO " . $this->getTableName()." ("; |
|
278 | foreach ($this->properties as $key => $val) { |
|
279 | $columnNames .= $key; |
|
280 | $columnValues .= ':' . $key; |
|
281 | $count++; |
|
282 | if ($count < count($this->properties)) |
|
283 | { |
|
284 | $columnNames .= ', '; |
|
285 | $columnValues .= ', '; |
|
286 | } |
|
287 | } |
|
288 | $create .= $columnNames.') VALUES (' .$columnValues.')'; |
|
289 | $stmt = $connection->prepare($create); |
|
290 | foreach ($this->properties as $key => $val) { |
|
291 | $stmt->bindValue(':'.$key, $val); |
|
292 | } |
|
293 | try { |
|
294 | // if prop returned and props from db differ throw exception |
|
295 | $stmt->execute(); |
|
296 | } catch(PDOException $e){ |
|
297 | return $e->getExceptionMessage(); |
|
298 | } |
|
299 | return $stmt->rowCount(); |
|
300 | } |
|
301 | /** |
|
302 | * get db connection |
|
303 | */ |
|
304 | public function getConnection($connection = null) |
|
305 | { |
|
306 | if(is_null($connection)) |
|
307 | { |
|
308 | return new Connection(); |
|
309 | } |
|
310 | } |
|
311 | /** |
|
312 | * checks if the id exists |
|
313 | * update if exist |
|
314 | * create if not exist |
|
315 | */ |
|
316 | public function save() |
|
317 | { |
|
318 | if ($this->id) { |
|
319 | $this->update(); |
|
320 | } else { |
|
321 | $this->create(); |
|
322 | } |
|
323 | } |
|
324 | /** |
|
325 | * @param row reps record id |
|
326 | * @param $connection initialised to null |
|
327 | * @return boolean |
|
328 | */ |
|
329 | public static function destroy($id) |
|
330 | { |
|
331 | ||
332 | $sql = "DELETE" . " FROM " . self::getTableName()." WHERE id = ". $id; |
|
333 | $delete = $connection->prepare($sql); |
|
334 | $delete->execute(); |
|
335 | $count = $delete->rowCount(); |
|
336 | if ($count < 1) { |
|
337 | throw new RecordNotFoundException('Record with id ' . $id . ' does not exist.'); |
|
338 | } |
|
339 | return ($count > 0) ? true : false; |
|
340 | } |
|
341 | ||
342 | /** |
|
343 | * Handle dynamic static method calls into the method. |
|
344 | * |
|
345 | * @param string $method |
|
346 | * @param array $parameters |
|
347 | * @return mixed |
|
348 | */ |
|
349 | public static function __callStatic($method, $parameters) |
|
350 | { |
|
351 | exit("HERE"); |
|
352 | $instance = new static; |
|
353 | ||
354 | return call_user_func_array([$instance, $method], $parameters); |
|
355 | } |
|
356 | } |
|
357 | ||
358 | interface ModelInterface { |
|
359 | /** |
@@ 5-220 (lines=216) @@ | ||
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 | * returns a particular record |
|
74 | * @param $id reps the record id |
|
75 | * @param $connection initialised to null |
|
76 | * @return object |
|
77 | */ |
|
78 | public function get($id) |
|
79 | { |
|
80 | $sql = "SELECT * FROM {$this->getTableName()} WHERE id={$id}"; |
|
81 | $sqlStatement = $this->databaseConnection->prepare($sql); |
|
82 | $sqlStatement->setFetchMode($this->databaseConnection::FETCH_CLASS, get_called_class()); |
|
83 | $sqlStatement->execute(); |
|
84 | if($sqlStatement->rowCount() < 1){ |
|
85 | throw new ModelNotFoundException($id); |
|
86 | } |
|
87 | return $sqlStatement->fetch(); |
|
88 | } |
|
89 | ||
90 | public static function getAll() |
|
91 | { |
|
92 | $model = new static; |
|
93 | return $model->all(); |
|
94 | } |
|
95 | ||
96 | public function all() |
|
97 | { |
|
98 | $sql = "SELECT * FROM {$this->getTableName()}"; |
|
99 | $row = $this->databaseConnection->prepare($sql); |
|
100 | $row->execute(); |
|
101 | ||
102 | return $row->fetchAll($this->databaseConnection::FETCH_CLASS); |
|
103 | ||
104 | } |
|
105 | /** update table with instance properties |
|
106 | * |
|
107 | */ |
|
108 | private function update() |
|
109 | { |
|
110 | $connection = $this->getConnection(); |
|
111 | $columnNames = ""; |
|
112 | $columnValues = ""; |
|
113 | $count = 0; |
|
114 | $update = "UPDATE " . $this->getTableName() . " SET " ; |
|
115 | foreach ($this->properties as $key => $val) { |
|
116 | $count++; |
|
117 | if(($key == 'id')) continue; |
|
118 | $update .= "$key = '$val'"; |
|
119 | if ($count < count($this->properties) ) |
|
120 | { |
|
121 | $update .=","; |
|
122 | } |
|
123 | } |
|
124 | $update .= " WHERE id = " . $this->properties['id']; |
|
125 | $stmt = $connection->prepare($update); |
|
126 | foreach ($this->properties as $key => $val) { |
|
127 | if($key == 'id') continue; |
|
128 | } |
|
129 | $stmt->execute(); |
|
130 | return $stmt->rowCount(); |
|
131 | } |
|
132 | /** |
|
133 | * insert instance data into the table |
|
134 | */ |
|
135 | private function create() |
|
136 | { |
|
137 | $connection = $this->getConnection(); |
|
138 | $columnNames = ""; |
|
139 | $columnValues = ""; |
|
140 | $count = 0; |
|
141 | $create = "INSERT" . " INTO " . $this->getTableName()." ("; |
|
142 | foreach ($this->properties as $key => $val) { |
|
143 | $columnNames .= $key; |
|
144 | $columnValues .= ':' . $key; |
|
145 | $count++; |
|
146 | if ($count < count($this->properties)) |
|
147 | { |
|
148 | $columnNames .= ', '; |
|
149 | $columnValues .= ', '; |
|
150 | } |
|
151 | } |
|
152 | $create .= $columnNames.') VALUES (' .$columnValues.')'; |
|
153 | $stmt = $connection->prepare($create); |
|
154 | foreach ($this->properties as $key => $val) { |
|
155 | $stmt->bindValue(':'.$key, $val); |
|
156 | } |
|
157 | try { |
|
158 | // if prop returned and props from db differ throw exception |
|
159 | $stmt->execute(); |
|
160 | } catch(PDOException $e){ |
|
161 | return $e->getExceptionMessage(); |
|
162 | } |
|
163 | return $stmt->rowCount(); |
|
164 | } |
|
165 | /** |
|
166 | * get db connection |
|
167 | */ |
|
168 | public function getConnection($connection = null) |
|
169 | { |
|
170 | if(is_null($connection)) |
|
171 | { |
|
172 | return new Connection(); |
|
173 | } |
|
174 | } |
|
175 | /** |
|
176 | * checks if the id exists |
|
177 | * update if exist |
|
178 | * create if not exist |
|
179 | */ |
|
180 | public function save() |
|
181 | { |
|
182 | if ($this->id) { |
|
183 | $this->update(); |
|
184 | } else { |
|
185 | $this->create(); |
|
186 | } |
|
187 | } |
|
188 | /** |
|
189 | * @param row reps record id |
|
190 | * @param $connection initialised to null |
|
191 | * @return boolean |
|
192 | */ |
|
193 | public static function destroy($id) |
|
194 | { |
|
195 | ||
196 | $sql = "DELETE" . " FROM " . self::getTableName()." WHERE id = ". $id; |
|
197 | $delete = $connection->prepare($sql); |
|
198 | $delete->execute(); |
|
199 | $count = $delete->rowCount(); |
|
200 | if ($count < 1) { |
|
201 | throw new RecordNotFoundException('Record with id ' . $id . ' does not exist.'); |
|
202 | } |
|
203 | return ($count > 0) ? true : false; |
|
204 | } |
|
205 | ||
206 | /** |
|
207 | * Handle dynamic static method calls into the method. |
|
208 | * |
|
209 | * @param string $method |
|
210 | * @param array $parameters |
|
211 | * @return mixed |
|
212 | */ |
|
213 | public static function __callStatic($method, $parameters) |
|
214 | { |
|
215 | exit("HERE"); |
|
216 | $instance = new static; |
|
217 | ||
218 | return call_user_func_array([$instance, $method], $parameters); |
|
219 | } |
|
220 | } |