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\Framework\Models\Sql\Traits; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class TraitModifier |
20
|
|
|
* |
21
|
|
|
* @package O2System\Framework\Models\Sql\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 table. |
30
|
|
|
* |
31
|
|
|
* @access public |
32
|
|
|
* |
33
|
|
|
* @param array $sets Array of Input Data |
34
|
|
|
* |
35
|
|
|
* @return mixed |
36
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
37
|
|
|
*/ |
38
|
|
|
protected function insert(array $sets) |
39
|
|
|
{ |
40
|
|
|
if (method_exists($this, 'insertRecordSets')) { |
41
|
|
|
$this->insertRecordSets($sets); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (method_exists($this, 'beforeInsert')) { |
45
|
|
|
$this->beforeInsert($sets); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (method_exists($this, 'getRecordOrdering')) { |
49
|
|
|
if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) { |
50
|
|
|
$sets[ 'record_ordering' ] = $this->getRecordOrdering($this->table); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($this->qb->table($this->table)->insert($sets)) { |
55
|
|
|
if (method_exists($this, 'afterInsert')) { |
56
|
|
|
return $this->afterInsert(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// ------------------------------------------------------------------------ |
66
|
|
|
|
67
|
|
|
protected function insertOrUpdate(array $sets) |
68
|
|
|
{ |
69
|
|
|
// Try to find |
70
|
|
|
if($result = $this->qb->from($this->table)->getWhere($sets)) { |
|
|
|
|
71
|
|
|
return $this->update($sets); |
72
|
|
|
} else { |
73
|
|
|
return $this->insert($sets); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return false; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function insertMany(array $sets) |
80
|
|
|
{ |
81
|
|
|
if (method_exists($this, 'insertRecordSets')) { |
82
|
|
|
foreach ($sets as $set) { |
83
|
|
|
$this->insertRecordSets($set); |
84
|
|
|
|
85
|
|
|
if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) { |
86
|
|
|
$set[ 'record_ordering' ] = $this->getRecordOrdering($this->table); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (method_exists($this, 'beforeInsertMany')) { |
92
|
|
|
$this->beforeInsertMany($sets); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($this->qb->table($this->table)->insertBatch($sets)) { |
96
|
|
|
if (method_exists($this, 'afterInsertMany')) { |
97
|
|
|
return $this->afterInsertMany(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Update Data |
108
|
|
|
* |
109
|
|
|
* Method to update data as well as equipping the data in accordance with the fields |
110
|
|
|
* in the destination database table. |
111
|
|
|
* |
112
|
|
|
* @access public |
113
|
|
|
* |
114
|
|
|
* @param array $sets Array of Update Data |
115
|
|
|
* |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
protected function update(array $sets, $where = []) |
119
|
|
|
{ |
120
|
|
|
$primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
121
|
|
|
|
122
|
|
|
if (empty($where)) { |
123
|
|
|
if (empty($this->primaryKeys)) { |
124
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
125
|
|
|
} else { |
126
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
127
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Reset Primary Keys |
133
|
|
|
$this->primaryKey = 'id'; |
|
|
|
|
134
|
|
|
$this->primaryKeys = []; |
|
|
|
|
135
|
|
|
|
136
|
|
|
if (method_exists($this, 'updateRecordSets')) { |
137
|
|
|
$this->updateRecordSets($sets); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (method_exists($this, 'beforeUpdate')) { |
141
|
|
|
$sets = $this->beforeUpdate($sets); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if (method_exists($this, 'getRecordOrdering')) { |
145
|
|
|
if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) { |
146
|
|
|
$sets[ 'record_ordering' ] = $this->getRecordOrdering($this->table); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if ($this->qb->table($this->table)->update($sets, $where)) { |
151
|
|
|
|
152
|
|
|
if (method_exists($this, 'afterUpdate')) { |
153
|
|
|
return $this->afterUpdate(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Find Or Insert |
164
|
|
|
* |
165
|
|
|
* To insert if there is no record before. |
166
|
|
|
* This is very important in insert to pivot table and avoid redundan |
167
|
|
|
* |
168
|
|
|
* @access public |
169
|
|
|
* @param array $sets Array of Input Data |
170
|
|
|
* @param array $sets Array of Reference |
171
|
|
|
* @return int |
172
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
173
|
|
|
*/ |
174
|
|
|
protected function findOrInsert(array $sets, array $reference = null) |
175
|
|
|
{ |
176
|
|
|
if ($reference != null) { |
177
|
|
|
// Disini where nya berdasarkan hasil define. |
178
|
|
|
$result = $this->qb->from($this->table)->getWhere($reference); |
179
|
|
|
} else { |
180
|
|
|
$result = $this->qb->from($this->table)->getWhere($sets); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if ($result->count() == 0) { |
184
|
|
|
$this->insert($sets); |
185
|
|
|
|
186
|
|
|
return $this->db->getLastInsertId(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $result[0]->id; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected function updateOrInsert(array $sets, array $where = []) |
193
|
|
|
{ |
194
|
|
|
$primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
195
|
|
|
|
196
|
|
|
if (empty($where)) { |
197
|
|
|
if (empty($this->primaryKeys)) { |
198
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
199
|
|
|
} else { |
200
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
201
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// Reset Primary Keys |
207
|
|
|
$this->primaryKey = 'id'; |
|
|
|
|
208
|
|
|
$this->primaryKeys = []; |
|
|
|
|
209
|
|
|
|
210
|
|
|
// Try to find |
211
|
|
|
if($result = $this->qb->from($this->table)->getWhere($where)) { |
|
|
|
|
212
|
|
|
return $this->update($sets, $where); |
213
|
|
|
} else { |
214
|
|
|
return $this->insert($sets); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return false; |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// ------------------------------------------------------------------------ |
221
|
|
|
|
222
|
|
|
protected function updateMany(array $sets) |
223
|
|
|
{ |
224
|
|
|
$primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
225
|
|
|
|
226
|
|
|
$where = []; |
227
|
|
|
if (empty($this->primaryKeys)) { |
228
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
229
|
|
|
$this->qb->where($primaryKey, $sets[ $primaryKey ]); |
230
|
|
|
} else { |
231
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
232
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// Reset Primary Keys |
237
|
|
|
$this->primaryKey = 'id'; |
|
|
|
|
238
|
|
|
$this->primaryKeys = []; |
|
|
|
|
239
|
|
|
|
240
|
|
|
if (method_exists($this, 'updateRecordSets')) { |
241
|
|
|
foreach ($sets as $set) { |
242
|
|
|
$this->updateRecordSets($set); |
243
|
|
|
|
244
|
|
|
if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) { |
245
|
|
|
$set[ 'record_ordering' ] = $this->getRecordOrdering($this->table); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (method_exists($this, 'beforeUpdateMany')) { |
251
|
|
|
$this->beforeUpdateMany($sets); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if ($this->qb->table($this->table)->updateBatch($sets, $where)) { |
255
|
|
|
if (method_exists($this, 'afterUpdateMany')) { |
256
|
|
|
return $this->afterUpdateMany(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return true; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return false; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
// ------------------------------------------------------------------------ |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* softDelete |
269
|
|
|
* |
270
|
|
|
* @param $id |
271
|
|
|
* |
272
|
|
|
* @return array|bool |
273
|
|
|
*/ |
274
|
|
|
protected function softDelete($id) |
275
|
|
|
{ |
276
|
|
|
$primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
277
|
|
|
|
278
|
|
|
$sets = []; |
279
|
|
|
$where = []; |
280
|
|
|
|
281
|
|
|
if (empty($this->primaryKeys)) { |
282
|
|
|
$where[ $primaryKey ] = $id; |
283
|
|
|
$sets[ $primaryKey ] = $id; |
284
|
|
|
} elseif (is_array($id)) { |
285
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
286
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
287
|
|
|
$sets[ $primaryKey ] = $id[ $primaryKey ]; |
288
|
|
|
} |
289
|
|
|
} else { |
290
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
291
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$sets[ reset($this->primaryKeys) ] = $id; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
// Reset Primary Keys |
298
|
|
|
$this->primaryKey = 'id'; |
|
|
|
|
299
|
|
|
$this->primaryKeys = []; |
|
|
|
|
300
|
|
|
|
301
|
|
|
if (method_exists($this, 'updateRecordSets')) { |
302
|
|
|
$this->setRecordStatus('DELETE'); |
|
|
|
|
303
|
|
|
$this->updateRecordSets($sets); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
if (method_exists($this, 'beforesoftDelete')) { |
307
|
|
|
$this->beforesoftDelete($sets); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
if ($this->qb->table($this->table)->update($sets, $where)) { |
311
|
|
|
if (method_exists($this, 'aftersoftDelete')) { |
312
|
|
|
return $this->aftersoftDelete(); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return true; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return false; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
// ------------------------------------------------------------------------ |
322
|
|
|
|
323
|
|
|
protected function softDeleteBy($id, array $where = []) |
324
|
|
|
{ |
325
|
|
|
$this->qb->where($where); |
326
|
|
|
|
327
|
|
|
return $this->softDelete($id); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
// ------------------------------------------------------------------------ |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* softDelete many rows from the database table based on sets of ids. |
334
|
|
|
* |
335
|
|
|
* @param array $ids |
336
|
|
|
* |
337
|
|
|
* @return mixed |
338
|
|
|
*/ |
339
|
|
|
protected function softDeleteMany(array $ids) |
340
|
|
|
{ |
341
|
|
|
$affectedRows = []; |
342
|
|
|
|
343
|
|
|
foreach ($ids as $id) { |
344
|
|
|
$affectedRows[ $id ] = $this->softDelete($id); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
return $affectedRows; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
// ------------------------------------------------------------------------ |
351
|
|
|
|
352
|
|
|
protected function softDeleteManyBy(array $ids, $where = []) |
353
|
|
|
{ |
354
|
|
|
$affectedRows = []; |
355
|
|
|
|
356
|
|
|
foreach ($ids as $id) { |
357
|
|
|
$affectedRows[ $id ] = $this->softDeleteBy($id, $where); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return $affectedRows; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
// ------------------------------------------------------------------------ |
364
|
|
|
|
365
|
|
|
protected function delete($id) |
366
|
|
|
{ |
367
|
|
|
$primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
368
|
|
|
|
369
|
|
|
$where = []; |
370
|
|
|
if (empty($this->primaryKeys)) { |
371
|
|
|
$where[ $primaryKey ] = $id; |
372
|
|
|
} elseif (is_array($id)) { |
373
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
374
|
|
|
$where[ $primaryKey ] = $id[ $primaryKey ]; |
375
|
|
|
} |
376
|
|
|
} else { |
377
|
|
|
$where[ reset($this->primaryKeys) ] = $id; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
// Reset Primary Keys |
381
|
|
|
$this->primaryKey = 'id'; |
|
|
|
|
382
|
|
|
$this->primaryKeys = []; |
|
|
|
|
383
|
|
|
|
384
|
|
|
if (method_exists($this, 'beforeDelete')) { |
385
|
|
|
$this->beforeDelete(); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
if ($this->qb->table($this->table)->delete($where)) { |
389
|
|
|
if (method_exists($this, 'afterDelete')) { |
390
|
|
|
return $this->afterDelete(); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
return true; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
return false; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
// ------------------------------------------------------------------------ |
400
|
|
|
|
401
|
|
|
protected function deleteBy($id, $where = [], $force = false) |
402
|
|
|
{ |
403
|
|
|
$this->qb->where($where); |
404
|
|
|
|
405
|
|
|
return $this->delete($id, $force); |
|
|
|
|
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
// ------------------------------------------------------------------------ |
409
|
|
|
|
410
|
|
|
protected function deleteMany(array $ids, $force = false) |
411
|
|
|
{ |
412
|
|
|
$affectedRows = []; |
413
|
|
|
|
414
|
|
|
foreach ($ids as $id) { |
415
|
|
|
$affectedRows[ $id ] = $this->delete($id, $force); |
|
|
|
|
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
return $affectedRows; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
protected function deleteManyBy(array $ids, $where = [], $force = false) |
422
|
|
|
{ |
423
|
|
|
$affectedRows = []; |
424
|
|
|
|
425
|
|
|
foreach ($ids as $id) { |
426
|
|
|
$affectedRows[ $id ] = $this->deleteBy($id, $where, $force); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
return $affectedRows; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
// ------------------------------------------------------------------------ |
433
|
|
|
|
434
|
|
|
protected function publish($id) |
435
|
|
|
{ |
436
|
|
|
$primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
437
|
|
|
|
438
|
|
|
$sets = []; |
439
|
|
|
$where = []; |
440
|
|
|
|
441
|
|
|
if (empty($this->primaryKeys)) { |
442
|
|
|
$where[ $primaryKey ] = $id; |
443
|
|
|
$sets[ $primaryKey ] = $id; |
444
|
|
|
} elseif (is_array($id)) { |
445
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
446
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
447
|
|
|
$sets[ $primaryKey ] = $id[ $primaryKey ]; |
448
|
|
|
} |
449
|
|
|
} else { |
450
|
|
|
foreach ($this->primaryKeys as $primaryKey) { |
451
|
|
|
$where[ $primaryKey ] = $sets[ $primaryKey ]; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
$sets[ reset($this->primaryKeys) ] = $id; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
// Reset Primary Keys |
458
|
|
|
$this->primaryKey = 'id'; |
|
|
|
|
459
|
|
|
$this->primaryKeys = []; |
|
|
|
|
460
|
|
|
|
461
|
|
|
if (method_exists($this, 'updateRecordSets')) { |
462
|
|
|
$this->setRecordStatus('PUBLISH'); |
463
|
|
|
$this->updateRecordSets($sets); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
if (method_exists($this, 'beforePublish')) { |
467
|
|
|
$this->beforePublish($sets); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
if ($this->qb->table($this->table)->update($sets, $where)) { |
471
|
|
|
if (method_exists($this, 'afterPublish')) { |
472
|
|
|
return $this->afterPublish(); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
return true; |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
return false; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
// ------------------------------------------------------------------------ |
482
|
|
|
|
483
|
|
|
protected function publishBy($id, array $where = []) |
484
|
|
|
{ |
485
|
|
|
$this->qb->where($where); |
486
|
|
|
|
487
|
|
|
return $this->publish($id); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
// ------------------------------------------------------------------------ |
491
|
|
|
|
492
|
|
|
protected function publishMany(array $ids) |
493
|
|
|
{ |
494
|
|
|
$affectedRows = []; |
495
|
|
|
|
496
|
|
|
foreach ($ids as $id) { |
497
|
|
|
$affectedRows[ $id ] = $this->publish($id); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
return $affectedRows; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
// ------------------------------------------------------------------------ |
504
|
|
|
|
505
|
|
|
protected function publishManyBy(array $ids, $where = []) |
506
|
|
|
{ |
507
|
|
|
$affectedRows = []; |
508
|
|
|
|
509
|
|
|
foreach ($ids as $id) { |
510
|
|
|
$affectedRows[ $id ] = $this->publishBy($id, $where); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
return $affectedRows; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
// ------------------------------------------------------------------------ |
517
|
|
|
|
518
|
|
|
protected function restore($id) |
519
|
|
|
{ |
520
|
|
|
return $this->publish($id); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
// ------------------------------------------------------------------------ |
524
|
|
|
|
525
|
|
|
protected function restoreBy($id, array $where = []) |
526
|
|
|
{ |
527
|
|
|
return $this->publishBy($id, $where); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
// ------------------------------------------------------------------------ |
531
|
|
|
|
532
|
|
|
protected function restoreMany(array $ids) |
533
|
|
|
{ |
534
|
|
|
return $this->publishMany($ids); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
// ------------------------------------------------------------------------ |
538
|
|
|
|
539
|
|
|
protected function restoreManyBy(array $ids, $where = []) |
540
|
|
|
{ |
541
|
|
|
return $this->publishManyBy($ids, $where); |
542
|
|
|
} |
543
|
|
|
} |