|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LRC\Repository; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Base class for database-backed repositories for data access. |
|
7
|
|
|
* |
|
8
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity) |
|
9
|
|
|
*/ |
|
10
|
|
|
class DbRepository extends ManagedRepository implements RepositoryInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Anax\Database\DatabaseQueryBuilder Database service. |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $db; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string Database table name. |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $table; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string Model class name. |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $modelClass; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string Primary key column. |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $key; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param \Anax\Database\DatabaseQueryBuilder $db Database service. |
|
37
|
|
|
* @param string $table Database table name. |
|
38
|
|
|
* @param string $modelClass Model class name. |
|
39
|
|
|
* @param string $key Primary key column. |
|
40
|
|
|
*/ |
|
41
|
30 |
|
public function __construct($db, $table, $modelClass, $key = 'id') |
|
42
|
|
|
{ |
|
43
|
30 |
|
$this->db = $db; |
|
44
|
30 |
|
$this->table = $table; |
|
45
|
30 |
|
$this->modelClass = $modelClass; |
|
46
|
30 |
|
$this->key = $key; |
|
47
|
30 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Return the name of the database table represented by the repository. |
|
52
|
|
|
*/ |
|
53
|
7 |
|
public function getCollectionName() |
|
54
|
|
|
{ |
|
55
|
7 |
|
return $this->table; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Return the class of the model handled by the repository. |
|
61
|
|
|
*/ |
|
62
|
14 |
|
public function getModelClass() |
|
63
|
|
|
{ |
|
64
|
14 |
|
return $this->modelClass; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Find and return first entry by key. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string|null $column Key column name (pass null to use registered primary key). |
|
72
|
|
|
* @param mixed $value Key value. |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed Model instance. |
|
75
|
|
|
*/ |
|
76
|
18 |
|
public function find($column, $value) |
|
77
|
|
|
{ |
|
78
|
18 |
|
return $this->getFirst((is_null($column) ? $this->key : $column) . ' = ?', [$value]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Retrieve first entry, optionally filtered by search criteria. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $conditions Where conditions. |
|
86
|
|
|
* @param array $values Array of condition values to bind. |
|
87
|
|
|
* @param array $options Query options. |
|
88
|
|
|
* |
|
89
|
|
|
* @return mixed Model instance. |
|
90
|
|
|
*/ |
|
91
|
19 |
|
public function getFirst($conditions = null, $values = [], $options = []) |
|
92
|
|
|
{ |
|
93
|
19 |
|
return $this->processSingleResult($this->executeQuery(null, $conditions, $values, $options)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Retrieve all entries, optionally filtered by search criteria. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $conditions Where conditions. |
|
101
|
|
|
* @param array $values Array of condition values to bind. |
|
102
|
|
|
* @param array $options Query options. |
|
103
|
|
|
* |
|
104
|
|
|
* @return array Array of all matching entries. |
|
105
|
|
|
*/ |
|
106
|
4 |
|
public function getAll($conditions = null, $values = [], $options = []) |
|
107
|
|
|
{ |
|
108
|
4 |
|
return $this->processMultipleResults($this->executeQuery(null, $conditions, $values, $options)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Save entry by inserting if ID is missing and updating if ID exists. |
|
114
|
|
|
* |
|
115
|
|
|
* @param mixed $model Model instance. |
|
116
|
|
|
* |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
5 |
|
public function save($model) |
|
120
|
|
|
{ |
|
121
|
5 |
|
if (isset($model->{$this->key})) { |
|
122
|
5 |
|
return $this->update($model); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
3 |
|
return $this->create($model); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Delete entry. |
|
131
|
|
|
* |
|
132
|
|
|
* @param mixed $model Model instance. |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function delete($model) |
|
135
|
|
|
{ |
|
136
|
1 |
|
$this->db->connect() |
|
137
|
1 |
|
->deleteFrom($this->table) |
|
138
|
1 |
|
->where($this->key . ' = ?') |
|
139
|
1 |
|
->execute([$model->{$this->key}]); |
|
|
|
|
|
|
140
|
1 |
|
$model->{$this->key} = null; |
|
141
|
1 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Count entries, optionally filtered by search criteria. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $conditions Where conditions. |
|
148
|
|
|
* @param array $values Array of condition values to bind. |
|
149
|
|
|
* |
|
150
|
|
|
* @return int Number of entries. |
|
151
|
|
|
*/ |
|
152
|
4 |
|
public function count($conditions = null, $values = []) |
|
153
|
|
|
{ |
|
154
|
4 |
|
$res = $this->executeQuery('COUNT(' . $this->key . ') AS num', $conditions, $values) |
|
155
|
4 |
|
->fetch(); |
|
156
|
4 |
|
return (isset($res->num) ? (int)$res->num : 0); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Execute query for selection methods. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $select Selection criteria. |
|
164
|
|
|
* @param string $conditions Where conditions. |
|
165
|
|
|
* @param array $values Array of where condition values to bind. |
|
166
|
|
|
* @param array $options Query options. |
|
167
|
|
|
* |
|
168
|
|
|
* @return \Anax\Database\DatabaseQueryBuilder Database service instance with executed internal query. |
|
169
|
|
|
*/ |
|
170
|
26 |
|
protected function executeQuery($select = null, $conditions = null, $values = [], $options = []) |
|
171
|
|
|
{ |
|
172
|
26 |
|
$query = $this->db->connect(); |
|
173
|
26 |
|
if (!empty($this->fetchRefs)) { |
|
174
|
6 |
|
$query = $this->setupJoin($query, $select, $conditions, (isset($options['order']) ? $options['order'] : null)); |
|
175
|
6 |
|
} else { |
|
176
|
26 |
|
$query = (!is_null($select) ? $query->select($select) : $query->select()); |
|
177
|
26 |
|
$query = $query->from($this->table); |
|
178
|
26 |
|
if (!is_null($conditions)) { |
|
179
|
26 |
|
$query = $query->where($conditions); |
|
180
|
26 |
|
} |
|
181
|
26 |
|
if (isset($options['order'])) { |
|
182
|
4 |
|
$query = $query->orderBy($options['order']); |
|
183
|
4 |
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
26 |
|
if (isset($options['limit'])) { |
|
187
|
2 |
|
$query = $query->limit($options['limit']); |
|
188
|
2 |
|
} |
|
189
|
26 |
|
if (isset($options['offset'])) { |
|
190
|
2 |
|
$query = $query->offset($options['offset']); |
|
191
|
2 |
|
} |
|
192
|
|
|
|
|
193
|
26 |
|
return $query->execute($values); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Populate model instance including retrieved references from join query result. |
|
200
|
|
|
* |
|
201
|
|
|
* @param object $result Query result. |
|
202
|
|
|
* |
|
203
|
|
|
* @return mixed Populated model instance. |
|
204
|
|
|
*/ |
|
205
|
6 |
|
protected function populateModelFromJoin($result) |
|
206
|
|
|
{ |
|
207
|
|
|
// extract main model |
|
208
|
6 |
|
$model = new $this->modelClass(); |
|
209
|
6 |
|
foreach (array_keys(get_object_vars($model)) as $attr) { |
|
210
|
6 |
|
$model->$attr = $result->$attr; |
|
211
|
6 |
|
} |
|
212
|
|
|
|
|
213
|
|
|
// extract referenced models |
|
214
|
6 |
|
$refs = $model->getReferences(); |
|
215
|
6 |
|
$refs2 = (is_array($this->fetchRefs) ? $this->fetchRefs : array_keys($refs)); |
|
216
|
6 |
|
sort($refs2); |
|
217
|
6 |
|
foreach ($refs2 as $idx => $name) { |
|
218
|
6 |
|
$prefix = "REF{$idx}_{$name}__"; |
|
219
|
|
|
|
|
220
|
|
|
// handle null result |
|
221
|
6 |
|
if (is_null($result->{$prefix . $refs[$name]['key']})) { |
|
222
|
3 |
|
$refModel = null; |
|
223
|
3 |
|
} else { |
|
224
|
6 |
|
$refModel = new $refs[$name]['model'](); |
|
225
|
6 |
|
foreach (array_keys(get_object_vars($refModel)) as $attr) { |
|
226
|
6 |
|
$refModel->$attr = $result->{$prefix . $attr}; |
|
227
|
6 |
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
// inject manager reference |
|
231
|
6 |
|
if ($refModel && $this->manager) { |
|
232
|
6 |
|
$this->manager->manageModel($refModel); |
|
233
|
6 |
|
} |
|
234
|
|
|
|
|
235
|
6 |
|
$model->$name = $refModel; |
|
236
|
6 |
|
} |
|
237
|
|
|
|
|
238
|
6 |
|
return $model; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Process single query result and return model instance. |
|
244
|
|
|
* |
|
245
|
|
|
* @param \Anax\Database\DatabaseQueryBuilder $query Database service instance with executed internal query. |
|
246
|
|
|
* |
|
247
|
|
|
* @return mixed Model instance. |
|
248
|
|
|
*/ |
|
249
|
22 |
View Code Duplication |
protected function processSingleResult($query) |
|
|
|
|
|
|
250
|
|
|
{ |
|
251
|
22 |
|
if (!empty($this->fetchRefs)) { |
|
252
|
4 |
|
$res = $query->fetch(); |
|
253
|
4 |
|
$model = ($res ? $this->populateModelFromJoin($res) : $res); |
|
|
|
|
|
|
254
|
4 |
|
} else { |
|
255
|
22 |
|
$model = $query->fetchClass($this->modelClass); |
|
|
|
|
|
|
256
|
|
|
} |
|
257
|
22 |
|
if ($model && isset($this->manager)) { |
|
258
|
10 |
|
$this->manager->manageModel($model); |
|
|
|
|
|
|
259
|
10 |
|
} |
|
260
|
22 |
|
$this->fetchReferences(false); |
|
261
|
22 |
|
return $model; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Process multiple query results and return model instances. |
|
267
|
|
|
* |
|
268
|
|
|
* @param \Anax\Database\DatabaseQueryBuilder $query Database service instance with executed internal query. |
|
269
|
|
|
* |
|
270
|
|
|
* @return array Array of model instances. |
|
271
|
|
|
*/ |
|
272
|
6 |
View Code Duplication |
protected function processMultipleResults($query) |
|
|
|
|
|
|
273
|
|
|
{ |
|
274
|
6 |
|
if (!empty($this->fetchRefs)) { |
|
275
|
3 |
|
$models = []; |
|
276
|
3 |
|
foreach ($query->fetchAll() as $model) { |
|
277
|
3 |
|
$models[] = $this->populateModelFromJoin($model); |
|
278
|
3 |
|
} |
|
279
|
3 |
|
} else { |
|
280
|
3 |
|
$models = $query->fetchAllClass($this->modelClass); |
|
|
|
|
|
|
281
|
|
|
} |
|
282
|
6 |
|
if (isset($this->manager)) { |
|
283
|
4 |
|
foreach ($models as $model) { |
|
284
|
4 |
|
$this->manager->manageModel($model); |
|
285
|
4 |
|
} |
|
286
|
4 |
|
} |
|
287
|
6 |
|
$this->fetchReferences(false); |
|
288
|
6 |
|
return $models; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Set up join query for reference retrieval. |
|
294
|
|
|
* |
|
295
|
|
|
* @param \Anax\Database\DatabaseQueryBuilder $query Database service instance with initialized query. |
|
296
|
|
|
* @param string $select Selection criteria. |
|
297
|
|
|
* @param string $conditions Where conditions. |
|
298
|
|
|
* @param string $order Order by clause. |
|
299
|
|
|
* |
|
300
|
|
|
* @return \Anax\Database\DatabaseQueryBuilder Database service instance with prepared join query. |
|
301
|
|
|
* |
|
302
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
303
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
|
304
|
|
|
*/ |
|
305
|
6 |
|
private function setupJoin($query, $select, $conditions, $order = null) |
|
306
|
|
|
{ |
|
307
|
|
|
// find references |
|
308
|
6 |
|
$model = new $this->modelClass(); |
|
309
|
6 |
|
$refs = $model->getReferences(); |
|
310
|
6 |
|
if (is_array($this->fetchRefs)) { |
|
311
|
5 |
|
$refs = array_intersect_key($refs, array_flip($this->fetchRefs)); |
|
312
|
5 |
|
} |
|
313
|
6 |
|
ksort($refs); |
|
314
|
|
|
|
|
315
|
|
|
// prefix main model selection |
|
316
|
6 |
|
if (!is_null($select)) { |
|
317
|
1 |
|
$select = $this->prefixModelAttributes($select, $model); |
|
318
|
1 |
|
} else { |
|
319
|
6 |
|
$select = $this->table . '.*'; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
// set up reference aliases and join conditions |
|
323
|
6 |
|
$select = [$select]; |
|
324
|
6 |
|
$join = []; |
|
325
|
6 |
|
$idx = 0; |
|
326
|
6 |
|
foreach ($refs as $name => $ref) { |
|
327
|
|
|
// prefix attributes |
|
328
|
6 |
|
$refTable = "REF{$idx}_{$name}"; |
|
329
|
6 |
|
$idx++; |
|
330
|
6 |
|
foreach (array_keys(get_object_vars(new $ref['model']())) as $attr) { |
|
331
|
6 |
|
$select[] = "{$refTable}.{$attr} AS '{$refTable}__{$attr}'"; |
|
332
|
6 |
|
} |
|
333
|
|
|
|
|
334
|
|
|
// generate join conditions |
|
335
|
6 |
|
$joinCond = $this->table . '.' . $ref['attribute'] . " = {$refTable}." . $ref['key']; |
|
336
|
6 |
|
$refRepo = $this->manager->getByClass($ref['model']); |
|
337
|
6 |
|
if ($this->softRefs && $refRepo instanceof SoftDbRepository) { |
|
338
|
3 |
|
$joinCond .= " AND $refTable." . $refRepo->getDeletedAttribute() . ' IS NULL'; |
|
339
|
3 |
|
} |
|
340
|
6 |
|
$join[] = [$refRepo->getCollectionName() . " AS $refTable", $joinCond]; |
|
341
|
6 |
|
} |
|
342
|
|
|
|
|
343
|
|
|
// generate join query |
|
344
|
6 |
|
$query = $query->select(implode(', ', $select))->from($this->table); |
|
345
|
6 |
|
foreach ($join as $args) { |
|
346
|
6 |
|
$query = $query->leftJoin($args[0], $args[1]); |
|
347
|
6 |
|
} |
|
348
|
|
|
|
|
349
|
|
|
// prefix where conditions |
|
350
|
6 |
|
if (!is_null($conditions)) { |
|
351
|
5 |
|
$query = $query->where($this->prefixModelAttributes($conditions, $model)); |
|
352
|
5 |
|
} |
|
353
|
|
|
|
|
354
|
|
|
// prefix order by clause |
|
355
|
6 |
|
if (!is_null($order)) { |
|
356
|
5 |
|
$query = $query->orderBy($this->prefixModelAttributes($order, $model)); |
|
357
|
5 |
|
} |
|
358
|
|
|
|
|
359
|
6 |
|
return $query; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Prefix model attributes with the associated table name. |
|
365
|
|
|
* |
|
366
|
|
|
* @param string $input Input string. |
|
367
|
|
|
* @param object $model Model instance. |
|
368
|
|
|
* |
|
369
|
|
|
* @return string String with table-prefixed attributes. |
|
370
|
|
|
*/ |
|
371
|
6 |
|
private function prefixModelAttributes($input, $model) |
|
372
|
|
|
{ |
|
373
|
6 |
|
foreach (array_keys(get_object_vars($model)) as $attr) { |
|
374
|
6 |
|
$input = preg_replace('/\\b' . $attr . '\\b/', $this->table . ".$attr", $input); |
|
375
|
6 |
|
} |
|
376
|
6 |
|
return $input; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* Create new entry. |
|
382
|
|
|
* |
|
383
|
|
|
* @param mixed $model Model instance. |
|
384
|
|
|
*/ |
|
385
|
3 |
|
private function create($model) |
|
386
|
|
|
{ |
|
387
|
3 |
|
$attrs = $this->getMutableAttributes($model); |
|
388
|
3 |
|
$this->db |
|
389
|
3 |
|
->connect() |
|
390
|
3 |
|
->insert($this->table, array_keys($attrs)) |
|
391
|
3 |
|
->execute(array_values($attrs)); |
|
|
|
|
|
|
392
|
3 |
|
$model->{$this->key} = $this->db->lastInsertId(); |
|
393
|
3 |
|
} |
|
394
|
|
|
|
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Update entry. |
|
398
|
|
|
* |
|
399
|
|
|
* @param mixed $model Model instance. |
|
400
|
|
|
*/ |
|
401
|
5 |
|
private function update($model) |
|
402
|
|
|
{ |
|
403
|
5 |
|
$attrs = $this->getMutableAttributes($model); |
|
404
|
5 |
|
$values = array_values($attrs); |
|
405
|
5 |
|
$values[] = $model->{$this->key}; |
|
406
|
5 |
|
$this->db |
|
407
|
5 |
|
->connect() |
|
408
|
5 |
|
->update($this->table, array_keys($attrs)) |
|
409
|
5 |
|
->where($this->key . ' = ?') |
|
410
|
5 |
|
->execute($values); |
|
|
|
|
|
|
411
|
5 |
|
} |
|
412
|
|
|
|
|
413
|
|
|
|
|
414
|
|
|
/** |
|
415
|
|
|
* Get mutable model attributes. |
|
416
|
|
|
* |
|
417
|
|
|
* @param object $model Model instance. |
|
418
|
|
|
* |
|
419
|
|
|
* @return array Array of attributes. |
|
420
|
|
|
*/ |
|
421
|
5 |
|
private function getMutableAttributes($model) |
|
422
|
|
|
{ |
|
423
|
5 |
|
$attrs = get_object_vars($model); |
|
424
|
5 |
|
unset($attrs[$this->key]); |
|
425
|
|
|
|
|
426
|
|
|
// remove reference attributes, if any |
|
427
|
5 |
|
if ($model instanceof ManagedModelInterface) { |
|
428
|
2 |
|
foreach (array_keys($model->getReferences()) as $ref) { |
|
429
|
2 |
|
unset($attrs[$ref]); |
|
430
|
2 |
|
} |
|
431
|
2 |
|
} |
|
432
|
|
|
|
|
433
|
5 |
|
return $attrs; |
|
434
|
|
|
} |
|
435
|
|
|
} |
|
436
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: