1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pyjac\ORM; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
6
|
|
|
use PDO; |
7
|
|
|
use Pyjac\ORM\Exception\ModelNotFoundException; |
8
|
|
|
|
9
|
|
|
abstract class Model implements ModelInterface |
10
|
|
|
{ |
11
|
|
|
protected $properties = []; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Store instance of database connection used. |
15
|
|
|
* |
16
|
|
|
* @var [type] |
17
|
|
|
*/ |
18
|
|
|
protected $databaseConnection; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The id of the model. |
22
|
|
|
* |
23
|
|
|
* @property string $id |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a model instance. |
28
|
|
|
*/ |
29
|
|
|
public function __construct(DatabaseConnectionInterface $databaseConnection = null) |
30
|
|
|
{ |
31
|
|
|
if ($databaseConnection == null) { |
32
|
|
|
$this->databaseConnection = DatabaseConnection::getInstance()->databaseConnection; |
33
|
|
|
} |
34
|
|
|
$this->databaseConnection = $databaseConnection; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Sets into $properties the $key => $value pairs. |
39
|
|
|
* |
40
|
|
|
* @param string $key |
41
|
|
|
* @param string $val |
42
|
|
|
*/ |
43
|
|
|
public function __set($key, $val) |
44
|
|
|
{ |
45
|
|
|
$this->properties[$key] = $val; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $key |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function __get($key) |
54
|
|
|
{ |
55
|
|
|
return $this->properties[$key]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get all the model properties. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function getProperties() |
64
|
|
|
{ |
65
|
|
|
return $this->properties; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set model properties. |
70
|
|
|
*/ |
71
|
|
|
public function setProperties(array $properties) |
72
|
|
|
{ |
73
|
|
|
$this->properties = $properties; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Pluralize the name of the child class. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getTableName() |
82
|
|
|
{ |
83
|
|
|
$className = explode('\\', get_called_class()); |
84
|
|
|
|
85
|
|
|
return Inflector::pluralize(strtolower(end($className))); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Find the particular model with the passed id. |
90
|
|
|
* |
91
|
|
|
* @param int $id |
92
|
|
|
* |
93
|
|
|
* @return object |
94
|
|
|
*/ |
95
|
|
|
public static function find($id) |
96
|
|
|
{ |
97
|
|
|
$model = new static(); |
98
|
|
|
|
99
|
|
|
return $model->get($id); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the particular model with the passed id. |
104
|
|
|
* |
105
|
|
|
* @param int $id |
106
|
|
|
* |
107
|
|
|
* @return object |
108
|
|
|
*/ |
109
|
|
|
public function get($id) |
110
|
|
|
{ |
111
|
|
|
$sql = "SELECT * FROM {$this->getTableName()} WHERE id={$id}"; |
112
|
|
|
$sqlStatement = $this->databaseConnection->prepare($sql); |
|
|
|
|
113
|
|
|
$sqlStatement->setFetchMode(PDO::FETCH_CLASS, get_called_class()); |
114
|
|
|
$sqlStatement->execute(); |
115
|
|
|
if ($sqlStatement->rowCount() < 1) { |
116
|
|
|
throw new ModelNotFoundException($id); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $sqlStatement->fetch(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get all the models from the database. |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public static function getAll() |
128
|
|
|
{ |
129
|
|
|
$model = new static(); |
130
|
|
|
|
131
|
|
|
return $model->all(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns all the models from the database. |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public function all() |
140
|
|
|
{ |
141
|
|
|
$sql = "SELECT * FROM {$this->getTableName()}"; |
142
|
|
|
$sqlStatement = $this->databaseConnection->prepare($sql); |
|
|
|
|
143
|
|
|
$sqlStatement->execute(); |
144
|
|
|
|
145
|
|
|
return $sqlStatement->fetchAll(PDO::FETCH_CLASS); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Update the model in the database. |
150
|
|
|
* |
151
|
|
|
* @return int |
152
|
|
|
*/ |
153
|
|
|
public function update() |
154
|
|
|
{ |
155
|
|
|
$bindNameParameters = []; |
156
|
|
|
$sqlUpdate = 'UPDATE '.$this->getTableName().' SET '; |
157
|
|
View Code Duplication |
foreach ($this->properties as $columnName => $columnValue) { |
|
|
|
|
158
|
|
|
if ($columnName == 'id') { |
159
|
|
|
continue; |
160
|
|
|
} |
161
|
|
|
$bindColumnName = ':'.$columnName; |
162
|
|
|
$sqlUpdate .= "$columnName = $bindColumnName,"; |
163
|
|
|
$bindNameParameters[$bindColumnName] = $columnValue; |
164
|
|
|
} |
165
|
|
|
//Remove the last comma in sql command then join it to the other query part. |
166
|
|
|
$sqlUpdate = substr($sqlUpdate, 0, -1).' WHERE id = :id'; |
167
|
|
|
$sqlStatement = $this->databaseConnection->prepare($sqlUpdate); |
|
|
|
|
168
|
|
|
$bindNameParameters[':id'] = $this->properties['id']; |
169
|
|
|
$sqlStatement->execute($bindNameParameters); |
170
|
|
|
|
171
|
|
|
return $sqlStatement->rowCount(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Insert the model values into the database. |
176
|
|
|
* |
177
|
|
|
* @return int |
178
|
|
|
*/ |
179
|
|
|
public function create() |
180
|
|
|
{ |
181
|
|
|
$columnNames = ''; |
182
|
|
|
$columnValues = ''; |
183
|
|
|
$bindNameParameters = []; |
184
|
|
|
$sqlCreate = 'INSERT'.' INTO '.$this->getTableName().' ('; |
185
|
|
View Code Duplication |
foreach ($this->properties as $columnName => $columnValue) { |
|
|
|
|
186
|
|
|
$bindColumnName = ':'.$columnName; |
187
|
|
|
$columnNames .= $columnName.','; |
188
|
|
|
$columnValues .= $bindColumnName.','; |
189
|
|
|
$bindNameParameters[$bindColumnName] = $columnValue; |
190
|
|
|
} |
191
|
|
|
// Remove ending comma and whitespace. |
192
|
|
|
$columnNames = substr($columnNames, 0, -1); |
193
|
|
|
$columnValues = substr($columnValues, 0, -1); |
194
|
|
|
|
195
|
|
|
$sqlCreate .= $columnNames.') VALUES ('.$columnValues.')'; |
196
|
|
|
$sqlStatement = $this->databaseConnection->prepare($sqlCreate); |
|
|
|
|
197
|
|
|
$sqlStatement->execute($bindNameParameters); |
198
|
|
|
|
199
|
|
|
return $sqlStatement->rowCount(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Save the model data to the database. |
204
|
|
|
* |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
|
|
public function save() |
208
|
|
|
{ |
209
|
|
|
return isset($this->properties['id']) ? $this->update() : $this->create(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Delete a model from the database. |
214
|
|
|
* |
215
|
|
|
* @param int $id |
216
|
|
|
* |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
|
|
public static function destroy($id) |
220
|
|
|
{ |
221
|
|
|
$model = new static(); |
222
|
|
|
|
223
|
|
|
return $model->delete($id); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Delete model from the database. |
228
|
|
|
* |
229
|
|
|
* @param int $id |
230
|
|
|
* |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
public function delete($id) |
234
|
|
|
{ |
235
|
|
|
$sql = 'DELETE'.' FROM '.self::getTableName().' WHERE id = '.$id; |
236
|
|
|
$sqlStatment = $this->databaseConnection->prepare($sql); |
|
|
|
|
237
|
|
|
$sqlStatment->execute(); |
238
|
|
|
|
239
|
|
|
return ($sqlStatment->rowCount() > 0) ? true : false; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.