1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Database; |
4
|
|
|
|
5
|
|
|
use \Anax\Database\DatabaseQueryBuilder; |
6
|
|
|
use \Anax\Database\Exception\ActiveRecordException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* An implementation of the Active Record pattern to be used as |
10
|
|
|
* base class for database driven models. |
11
|
|
|
* |
12
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
13
|
|
|
*/ |
14
|
|
|
class ActiveRecordModel |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var DatabaseQueryBuilder $db the object for persistent |
18
|
|
|
* storage. |
19
|
|
|
*/ |
20
|
|
|
protected $db = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string $tableName name of the database table. |
24
|
|
|
*/ |
25
|
|
|
protected $tableName = null; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set the database object to use for accessing storage. |
31
|
|
|
* |
32
|
|
|
* @param DatabaseQueryBuilder $db as database access object. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function setDb(DatabaseQueryBuilder $db) |
37
|
|
|
{ |
38
|
|
|
$this->db = $db; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check if database is injected or throw an exception. |
45
|
|
|
* |
46
|
|
|
* @throws ActiveRecordException when database is not set. |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
protected function checkDb() |
51
|
|
|
{ |
52
|
|
|
if (!$this->db) { |
53
|
|
|
throw new ActiveRecordException("Missing \$db, did you forget to inject/set is?"); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get essential object properties. |
61
|
|
|
* |
62
|
|
|
* @return array with object properties. |
63
|
|
|
*/ |
64
|
|
|
protected function getProperties() |
65
|
|
|
{ |
66
|
|
|
$properties = get_object_vars($this); |
67
|
|
|
unset($properties['tableName']); |
68
|
|
|
unset($properties['db']); |
69
|
|
|
unset($properties['di']); |
70
|
|
|
return $properties; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Find and return first object found by search criteria and use |
77
|
|
|
* its data to populate this instance. |
78
|
|
|
* |
79
|
|
|
* @param string $column to use in where statement. |
80
|
|
|
* @param mixed $value to use in where statement. |
81
|
|
|
* |
82
|
|
|
* @return this |
83
|
|
|
*/ |
84
|
|
|
public function find($column, $value) |
85
|
|
|
{ |
86
|
|
|
return $this->findWhere("$column = ?", $value); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Find and return first object by its tableIdColumn and use |
93
|
|
|
* its data to populate this instance. |
94
|
|
|
* |
95
|
|
|
* @param integer $id to find or use $this->{$this->tableIdColumn} |
96
|
|
|
* as default. |
97
|
|
|
* |
98
|
|
|
* @return this |
99
|
|
|
*/ |
100
|
|
|
public function findById($id = null) |
101
|
|
|
{ |
102
|
|
|
$id = $id ?: $this->{$this->tableIdColumn}; |
|
|
|
|
103
|
|
|
return $this->findWhere("{$this->tableIdColumn} = ?", $id); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Find and return first object found by search criteria and use |
110
|
|
|
* its data to populate this instance. |
111
|
|
|
* |
112
|
|
|
* The search criteria `$where` of can be set up like this: |
113
|
|
|
* `id = ?` |
114
|
|
|
* `id1 = ? and id2 = ?` |
115
|
|
|
* |
116
|
|
|
* The `$value` can be a single value or an array of values. |
117
|
|
|
* |
118
|
|
|
* @param string $where to use in where statement. |
119
|
|
|
* @param mixed $value to use in where statement. |
120
|
|
|
* |
121
|
|
|
* @return this |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function findWhere($where, $value) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$this->checkDb(); |
126
|
|
|
$params = is_array($value) ? $value : [$value]; |
127
|
|
|
return $this->db->connect() |
128
|
|
|
->select() |
129
|
|
|
->from($this->tableName) |
130
|
|
|
->where($where) |
131
|
|
|
->execute($params) |
|
|
|
|
132
|
|
|
->fetchInto($this); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Find and return all. |
139
|
|
|
* |
140
|
|
|
* @return array of object of this class |
141
|
|
|
*/ |
142
|
|
|
public function findAll() |
143
|
|
|
{ |
144
|
|
|
$this->checkDb(); |
145
|
|
|
return $this->db->connect() |
146
|
|
|
->select() |
147
|
|
|
->from($this->tableName) |
148
|
|
|
->execute() |
149
|
|
|
->fetchAllClass(get_class($this)); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
|
154
|
|
View Code Duplication |
public function findAllLimitOrderBy($order, $number) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$this->checkDb(); |
157
|
|
|
return $this->db->connect() |
158
|
|
|
->select() |
159
|
|
|
->from($this->tableName) |
160
|
|
|
->orderBy($order) |
161
|
|
|
->limit($number) |
162
|
|
|
->execute() |
163
|
|
|
->fetchAllClass(get_class($this)); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
public function findAllLimit($number) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$this->checkDb(); |
169
|
|
|
return $this->db->connect() |
170
|
|
|
->select() |
171
|
|
|
->from($this->tableName) |
172
|
|
|
->limit($number) |
173
|
|
|
->execute() |
174
|
|
|
->fetchAllClass(get_class($this)); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Find and return all matching the search criteria. |
182
|
|
|
* |
183
|
|
|
* The search criteria `$where` of can be set up like this: |
184
|
|
|
* `id = ?` |
185
|
|
|
* `id IN [?, ?]` |
186
|
|
|
* |
187
|
|
|
* The `$value` can be a single value or an array of values. |
188
|
|
|
* |
189
|
|
|
* @param string $where to use in where statement. |
190
|
|
|
* @param mixed $value to use in where statement. |
191
|
|
|
* |
192
|
|
|
* @return array of object of this class |
193
|
|
|
*/ |
194
|
|
View Code Duplication |
public function findAllWhere($where, $value) |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
$this->checkDb(); |
197
|
|
|
$params = is_array($value) ? $value : [$value]; |
198
|
|
|
return $this->db->connect() |
199
|
|
|
->select() |
200
|
|
|
->from($this->tableName) |
201
|
|
|
->where($where) |
202
|
|
|
->execute($params) |
|
|
|
|
203
|
|
|
->fetchAllClass(get_class($this)); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Execute rawsql |
209
|
|
|
* |
210
|
|
|
* @return array |
211
|
|
|
*/ |
212
|
|
|
public function findAllSql($sql, $params = []) |
213
|
|
|
{ |
214
|
|
|
$this->checkDb(); |
215
|
|
|
return $this->db->connect() |
216
|
|
|
->execute($sql, $params) |
217
|
|
|
->fetchAllClass(get_class($this)); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
public function next() |
222
|
|
|
{ |
223
|
|
|
return $this->db->next(); |
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Execute rawsql |
229
|
|
|
* |
230
|
|
|
* @return array |
231
|
|
|
*/ |
232
|
|
|
public function findAllSqlTest($sql, $params) |
233
|
|
|
{ |
234
|
|
|
$this->checkDb(); |
235
|
|
|
return $this->db->connect() |
236
|
|
|
->executeFetchAll($sql, $params); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Save current object/row, insert if id is missing and do an |
243
|
|
|
* update if the id exists. |
244
|
|
|
* |
245
|
|
|
* @return void |
246
|
|
|
*/ |
247
|
|
|
public function save($idName = null, $id = null) |
248
|
|
|
{ |
249
|
|
|
if (isset($this->id)) { |
250
|
|
|
return $this->update(); |
251
|
|
|
} elseif ($idName !== null) { |
252
|
|
|
return $this->update($idName, $id); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $this->create(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Create new row. |
262
|
|
|
* |
263
|
|
|
* @return void |
264
|
|
|
*/ |
265
|
|
|
protected function create() |
266
|
|
|
{ |
267
|
|
|
$this->checkDb(); |
268
|
|
|
$properties = $this->getProperties(); |
269
|
|
|
unset($properties['id']); |
270
|
|
|
$columns = array_keys($properties); |
271
|
|
|
$values = array_values($properties); |
272
|
|
|
|
273
|
|
|
$this->db->connect() |
274
|
|
|
->insert($this->tableName, $columns) |
275
|
|
|
->execute($values); |
|
|
|
|
276
|
|
|
|
277
|
|
|
$this->id = $this->db->lastInsertId(); |
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Update row. |
284
|
|
|
* |
285
|
|
|
* @return void |
286
|
|
|
*/ |
287
|
|
|
protected function update($idName = null, $id = null) |
288
|
|
|
{ |
289
|
|
|
$this->checkDb(); |
290
|
|
|
$properties = $this->getProperties(); |
291
|
|
|
unset($properties['id']); |
292
|
|
|
$columns = array_keys($properties); |
293
|
|
|
$values = array_values($properties); |
294
|
|
|
$values[] = isset($this->id) ? $this->id : $id ; |
295
|
|
|
$setId = $idName !== null ? $idName : "id"; |
296
|
|
|
|
297
|
|
|
$this->db->connect() |
298
|
|
|
->update($this->tableName, $columns) |
299
|
|
|
->where("$setId = ?") |
300
|
|
|
->execute($values); |
|
|
|
|
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Delete row. |
307
|
|
|
* |
308
|
|
|
* @param integer $id to delete or use $this->id as default. |
309
|
|
|
* |
310
|
|
|
* @return void |
311
|
|
|
*/ |
312
|
|
|
public function delete($idName = null, $id = null) |
313
|
|
|
{ |
314
|
|
|
$this->checkDb(); |
315
|
|
|
$id = $id ?: $this->id; |
316
|
|
|
$setId = $idName !== null ? $idName : "id"; |
317
|
|
|
|
318
|
|
|
$this->db->connect() |
319
|
|
|
->deleteFrom($this->tableName) |
320
|
|
|
->where("$setId = ?") |
321
|
|
|
->execute([$id]); |
|
|
|
|
322
|
|
|
|
323
|
|
|
$this->id = null; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
|
327
|
|
|
public function lastInsertId() |
328
|
|
|
{ |
329
|
|
|
return $this->db->lastInsertId(); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: