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