1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Reactor\Models\NoSql\Traits; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class TraitModifier |
20
|
|
|
* |
21
|
|
|
* @package O2System\Reactor\Models\NoSql\Traits |
22
|
|
|
*/ |
23
|
|
|
trait ModifierTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Insert Data |
27
|
|
|
* |
28
|
|
|
* Method to input data as well as equipping the data in accordance with the fields |
29
|
|
|
* in the destination database collection. |
30
|
|
|
* |
31
|
|
|
* @access public |
32
|
|
|
* |
33
|
|
|
* @param array $sets Array of Input Data |
34
|
|
|
* @param string $collection Table Name |
35
|
|
|
* |
36
|
|
|
* @return mixed |
37
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
38
|
|
|
*/ |
39
|
|
|
public function insert(array $sets, $collection = null) |
40
|
|
|
{ |
41
|
|
|
$collection = isset($collection) ? $collection : $this->collection; |
42
|
|
|
|
43
|
|
|
if (method_exists($this, 'insertRecordSets')) { |
44
|
|
|
$this->insertRecordSets($sets); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (method_exists($this, 'beforeInsert')) { |
48
|
|
|
$this->beforeInsert($sets); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($this->qb->collection($collection)->insert($sets)) { |
52
|
|
|
if (method_exists($this, 'afterInsert')) { |
53
|
|
|
return $this->afterInsert(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// ------------------------------------------------------------------------ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Update Data |
66
|
|
|
* |
67
|
|
|
* Method to update data as well as equipping the data in accordance with the fields |
68
|
|
|
* in the destination database collection. |
69
|
|
|
* |
70
|
|
|
* @access public |
71
|
|
|
* |
72
|
|
|
* @param string $collection Table Name |
73
|
|
|
* @param array $sets Array of Update Data |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function update(array $sets, $where = [], $collection = null) |
78
|
|
|
{ |
79
|
|
|
$collection = isset($collection) ? $collection : $this->collection; |
80
|
|
|
$primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
81
|
|
|
|
82
|
|
|
if (empty($where)) { |
83
|
|
|
if (empty($this->primaryKeys)) { |
84
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
85
|
|
|
} else { |
86
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
87
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Reset Primary Keys |
93
|
|
|
$this->primaryKey = 'id'; |
|
|
|
|
94
|
|
|
$this->primaryKeys = []; |
|
|
|
|
95
|
|
|
|
96
|
|
|
if (method_exists($this, 'updateRecordSets')) { |
97
|
|
|
$this->updateRecordSets($sets); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (method_exists($this, 'beforeUpdate')) { |
101
|
|
|
$sets = $this->beforeUpdate($sets); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($this->qb->collection($collection)->update($sets, $where)) { |
105
|
|
|
|
106
|
|
|
if (method_exists($this, 'afterUpdate')) { |
107
|
|
|
return $this->afterUpdate(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function insertMany(array $sets) |
117
|
|
|
{ |
118
|
|
|
$collection = isset($collection) ? $collection : $this->collection; |
|
|
|
|
119
|
|
|
|
120
|
|
|
if (method_exists($this, 'insertRecordSets')) { |
121
|
|
|
foreach($sets as $set) { |
122
|
|
|
$this->insertRecordSets($set); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (method_exists($this, 'beforeInsertMany')) { |
127
|
|
|
$this->beforeInsertMany($sets); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($this->qb->collection($collection)->insertBatch($sets)) { |
131
|
|
|
if (method_exists($this, 'afterInsertMany')) { |
132
|
|
|
return $this->afterInsertMany(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// ------------------------------------------------------------------------ |
142
|
|
|
|
143
|
|
|
public function updateMany(array $sets) |
144
|
|
|
{ |
145
|
|
|
$collection = isset($collection) ? $collection : $this->collection; |
|
|
|
|
146
|
|
|
$primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
147
|
|
|
|
148
|
|
|
$where = []; |
149
|
|
|
if (empty($this->primaryKeys)) { |
150
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
151
|
|
|
$this->qb->where($primaryKey, $sets[ $primaryKey ]); |
152
|
|
|
} else { |
153
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
154
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Reset Primary Keys |
159
|
|
|
$this->primaryKey = 'id'; |
|
|
|
|
160
|
|
|
$this->primaryKeys = []; |
|
|
|
|
161
|
|
|
|
162
|
|
|
if (method_exists($this, 'updateRecordSets')) { |
163
|
|
|
foreach($sets as $set) { |
164
|
|
|
$this->updateRecordSets($set); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (method_exists($this, 'beforeUpdateMany')) { |
169
|
|
|
$this->beforeUpdateMany($sets); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($this->qb->collection($collection)->updateBatch($sets, $where)) { |
173
|
|
|
if (method_exists($this, 'afterUpdateMany')) { |
174
|
|
|
return $this->afterUpdateMany(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return true; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// ------------------------------------------------------------------------ |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* trash |
187
|
|
|
* |
188
|
|
|
* @param $id |
189
|
|
|
* @param null $collection |
|
|
|
|
190
|
|
|
* |
191
|
|
|
* @return array|bool |
192
|
|
|
*/ |
193
|
|
|
public function trash($id, $collection = null) |
194
|
|
|
{ |
195
|
|
|
$collection = isset($collection) ? $collection : $this->collection; |
196
|
|
|
$primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
197
|
|
|
|
198
|
|
|
$sets = []; |
199
|
|
|
$where = []; |
200
|
|
|
|
201
|
|
|
if (empty($this->primaryKeys)) { |
202
|
|
|
$where[ $primaryKey ] = $id; |
203
|
|
|
$sets[ $primaryKey ] = $id; |
204
|
|
|
} elseif (is_array($id)) { |
205
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
206
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
207
|
|
|
$sets[ $primaryKey ] = $id[ $primaryKey ]; |
208
|
|
|
} |
209
|
|
|
} else { |
210
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
211
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$sets[ reset($this->primaryKeys) ] = $id; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// Reset Primary Keys |
218
|
|
|
$this->primaryKey = 'id'; |
|
|
|
|
219
|
|
|
$this->primaryKeys = []; |
|
|
|
|
220
|
|
|
|
221
|
|
|
if (method_exists($this, 'updateRecordSets')) { |
222
|
|
|
$this->setRecordStatus('TRASH'); |
|
|
|
|
223
|
|
|
$this->updateRecordSets($sets); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
if (method_exists($this, 'beforeTrash')) { |
227
|
|
|
$this->beforeTrash($sets); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if ($this->qb->collection($collection)->update($sets, $where)) { |
231
|
|
|
if (method_exists($this, 'afterTrash')) { |
232
|
|
|
return $this->afterTrash(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return true; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return false; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
// ------------------------------------------------------------------------ |
242
|
|
|
|
243
|
|
|
public function trashBy($id, array $where = [], $collection = null) |
244
|
|
|
{ |
245
|
|
|
$this->qb->where($where); |
246
|
|
|
|
247
|
|
|
return $this->trash($id, $collection); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// ------------------------------------------------------------------------ |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Trash many rows from the database collection based on sets of ids. |
254
|
|
|
* |
255
|
|
|
* @param array $ids |
256
|
|
|
* |
257
|
|
|
* @return mixed |
258
|
|
|
*/ |
259
|
|
|
public function trashMany(array $ids) |
260
|
|
|
{ |
261
|
|
|
$affectedRows = []; |
262
|
|
|
|
263
|
|
|
foreach ($ids as $id) { |
264
|
|
|
$affectedRows[ $id ] = $this->trash($id); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return $affectedRows; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
// ------------------------------------------------------------------------ |
271
|
|
|
|
272
|
|
|
public function trashManyBy(array $ids, $where = [], $collection = null) |
273
|
|
|
{ |
274
|
|
|
$affectedRows = []; |
275
|
|
|
|
276
|
|
|
foreach ($ids as $id) { |
277
|
|
|
$affectedRows[ $id ] = $this->trashBy($id, $where, $collection); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $affectedRows; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
// ------------------------------------------------------------------------ |
284
|
|
|
|
285
|
|
|
public function delete($id, $force = false, $collection = null) |
|
|
|
|
286
|
|
|
{ |
287
|
|
|
if ((isset($collection) AND is_bool($collection)) OR ! isset($collection)) { |
288
|
|
|
$collection = $this->collection; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
$primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
292
|
|
|
|
293
|
|
|
$where = []; |
294
|
|
|
if (empty($this->primaryKeys)) { |
295
|
|
|
$where[ $primaryKey ] = $id; |
296
|
|
|
} elseif (is_array($id)) { |
297
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
298
|
|
|
$where[ $primaryKey ] = $id[ $primaryKey ]; |
299
|
|
|
} |
300
|
|
|
} else { |
301
|
|
|
$where[ reset($this->primaryKeys) ] = $id; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
// Reset Primary Keys |
305
|
|
|
$this->primaryKey = 'id'; |
|
|
|
|
306
|
|
|
$this->primaryKeys = []; |
|
|
|
|
307
|
|
|
|
308
|
|
|
if (method_exists($this, 'beforeDelete')) { |
309
|
|
|
$this->beforeDelete(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if ($this->qb->collection($collection)->delete($where)) { |
313
|
|
|
if (method_exists($this, 'afterDelete')) { |
314
|
|
|
return $this->afterDelete(); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return true; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
return false; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
// ------------------------------------------------------------------------ |
324
|
|
|
|
325
|
|
|
public function deleteBy($id, $where = [], $force = false, $collection = null) |
326
|
|
|
{ |
327
|
|
|
$this->qb->where($where); |
328
|
|
|
|
329
|
|
|
return $this->delete($id, $force, $collection); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
// ------------------------------------------------------------------------ |
333
|
|
|
|
334
|
|
|
public function deleteMany(array $ids, $force = false, $collection = null) |
335
|
|
|
{ |
336
|
|
|
$affectedRows = []; |
337
|
|
|
|
338
|
|
|
foreach ($ids as $id) { |
339
|
|
|
$affectedRows[ $id ] = $this->delete($id, $force, $collection); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
return $affectedRows; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function deleteManyBy(array $ids, $where = [], $force = false, $collection = null) |
346
|
|
|
{ |
347
|
|
|
$affectedRows = []; |
348
|
|
|
|
349
|
|
|
foreach ($ids as $id) { |
350
|
|
|
$affectedRows[ $id ] = $this->deleteBy($id, $where, $force, $collection); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
return $affectedRows; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
// ------------------------------------------------------------------------ |
357
|
|
|
|
358
|
|
|
public function publish($id, $collection = null) |
359
|
|
|
{ |
360
|
|
|
$collection = isset($collection) ? $collection : $this->collection; |
361
|
|
|
$primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
362
|
|
|
|
363
|
|
|
$sets = []; |
364
|
|
|
$where = []; |
365
|
|
|
|
366
|
|
|
if (empty($this->primaryKeys)) { |
367
|
|
|
$where[ $primaryKey ] = $id; |
368
|
|
|
$sets[ $primaryKey ] = $id; |
369
|
|
|
} elseif (is_array($id)) { |
370
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
371
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
372
|
|
|
$sets[ $primaryKey ] = $id[ $primaryKey ]; |
373
|
|
|
} |
374
|
|
|
} else { |
375
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
376
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
$sets[ reset($this->primaryKeys) ] = $id; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
// Reset Primary Keys |
383
|
|
|
$this->primaryKey = 'id'; |
|
|
|
|
384
|
|
|
$this->primaryKeys = []; |
|
|
|
|
385
|
|
|
|
386
|
|
|
if (method_exists($this, 'updateRecordSets')) { |
387
|
|
|
$this->setRecordStatus('PUBLISH'); |
388
|
|
|
$this->updateRecordSets($sets); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
if (method_exists($this, 'beforePublish')) { |
392
|
|
|
$this->beforePublish($sets); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
if ($this->qb->collection($collection)->update($sets, $where)) { |
396
|
|
|
if (method_exists($this, 'afterPublish')) { |
397
|
|
|
return $this->afterPublish(); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
return true; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
return false; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
// ------------------------------------------------------------------------ |
407
|
|
|
|
408
|
|
|
public function publishBy($id, array $where = [], $collection = null) |
409
|
|
|
{ |
410
|
|
|
$this->qb->where($where); |
411
|
|
|
|
412
|
|
|
return $this->publish($id, $collection); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
// ------------------------------------------------------------------------ |
416
|
|
|
|
417
|
|
|
public function publishMany(array $ids, $collection = null) |
418
|
|
|
{ |
419
|
|
|
$affectedRows = []; |
420
|
|
|
|
421
|
|
|
foreach ($ids as $id) { |
422
|
|
|
$affectedRows[ $id ] = $this->publish($id, $collection); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
return $affectedRows; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
// ------------------------------------------------------------------------ |
429
|
|
|
|
430
|
|
|
public function publishManyBy(array $ids, $where = [], $collection = null) |
431
|
|
|
{ |
432
|
|
|
$affectedRows = []; |
433
|
|
|
|
434
|
|
|
foreach ($ids as $id) { |
435
|
|
|
$affectedRows[ $id ] = $this->publishBy($id, $where, $collection); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
return $affectedRows; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
// ------------------------------------------------------------------------ |
442
|
|
|
|
443
|
|
|
public function restore($id, $collection = null) |
444
|
|
|
{ |
445
|
|
|
return $this->publish($id, $collection); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
// ------------------------------------------------------------------------ |
449
|
|
|
|
450
|
|
|
public function restoreBy($id, array $where = [], $collection = null) |
451
|
|
|
{ |
452
|
|
|
return $this->publishBy($id, $where, $collection); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
// ------------------------------------------------------------------------ |
456
|
|
|
|
457
|
|
|
public function restoreMany(array $ids, $collection = null) |
458
|
|
|
{ |
459
|
|
|
return $this->publishMany($ids, $collection); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
// ------------------------------------------------------------------------ |
463
|
|
|
|
464
|
|
|
public function restoreManyBy(array $ids, $where = [], $collection = null) |
465
|
|
|
{ |
466
|
|
|
return $this->publishManyBy($ids, $where, $collection); |
467
|
|
|
} |
468
|
|
|
} |