|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipbox/spark/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipbox/spark |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\spark\services\traits; |
|
10
|
|
|
|
|
11
|
|
|
use flipbox\spark\helpers\RecordHelper; |
|
12
|
|
|
use flipbox\spark\models\Model; |
|
13
|
|
|
use flipbox\spark\records\Record; |
|
14
|
|
|
use yii\base\ModelEvent; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @package flipbox\spark\services\traits |
|
18
|
|
|
* @author Flipbox Factory <[email protected]> |
|
19
|
|
|
* @since 1.0.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
trait ModelDelete |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/******************************************* |
|
25
|
|
|
* ABSTRACTS |
|
26
|
|
|
*******************************************/ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param int $id |
|
30
|
|
|
* @param string|null $toScenario |
|
31
|
|
|
* @return Record |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract public function getRecordById(int $id, string $toScenario = null): Record; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/******************************************* |
|
37
|
|
|
* DELETE |
|
38
|
|
|
*******************************************/ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Model $model |
|
42
|
|
|
* @return bool |
|
43
|
|
|
* @throws \Exception |
|
44
|
|
|
*/ |
|
45
|
|
|
public function delete(Model $model): bool |
|
46
|
|
|
{ |
|
47
|
|
|
|
|
48
|
|
|
// The event to trigger |
|
49
|
|
|
$event = new ModelEvent(); |
|
50
|
|
|
|
|
51
|
|
|
// Db transaction |
|
52
|
|
|
$transaction = RecordHelper::beginTransaction(); |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
|
|
|
|
56
|
|
|
// The 'before' event |
|
57
|
|
|
if (!$model->beforeDelete($event)) { |
|
58
|
|
|
|
|
59
|
|
|
$transaction->rollBack(); |
|
60
|
|
|
|
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Get record |
|
65
|
|
|
$record = $this->getRecordById($model->id); |
|
66
|
|
|
|
|
67
|
|
|
// Insert record |
|
68
|
|
|
if (!$record->delete()) { |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
// Transfer errors to model |
|
71
|
|
|
$model->addErrors($record->getErrors()); |
|
72
|
|
|
|
|
73
|
|
|
// Roll back db transaction |
|
74
|
|
|
$transaction->rollBack(); |
|
75
|
|
|
|
|
76
|
|
|
return false; |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// The 'after' event |
|
81
|
|
|
if (!$model->afterDelete($event)) { |
|
82
|
|
|
|
|
83
|
|
|
// Roll back db transaction |
|
84
|
|
|
$transaction->rollBack(); |
|
85
|
|
|
|
|
86
|
|
|
return false; |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} catch (\Exception $e) { |
|
91
|
|
|
|
|
92
|
|
|
// Roll back all db actions (fail) |
|
93
|
|
|
$transaction->rollback(); |
|
94
|
|
|
|
|
95
|
|
|
throw $e; |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$transaction->commit(); |
|
100
|
|
|
|
|
101
|
|
|
return true; |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: