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