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 Craft; |
12
|
|
|
use flipbox\spark\helpers\RecordHelper; |
13
|
|
|
use flipbox\spark\models\Model; |
14
|
|
|
use flipbox\spark\records\Record; |
15
|
|
|
use yii\base\ModelEvent; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @package flipbox\spark\services\traits |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
trait ModelDelete |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/******************************************* |
26
|
|
|
* ABSTRACTS |
27
|
|
|
*******************************************/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param int $id |
31
|
|
|
* @param string|null $toScenario |
32
|
|
|
* @return Record |
33
|
|
|
*/ |
34
|
|
|
abstract public function getRecordById(int $id, string $toScenario = null): Record; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/******************************************* |
38
|
|
|
* DELETE |
39
|
|
|
*******************************************/ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Model $model |
43
|
|
|
* @return bool |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
|
|
public function delete(Model $model): bool |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
// a 'beforeSave' event |
50
|
|
|
if(!$this->beforeDelete($model)) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// The event to trigger |
55
|
|
|
$event = new ModelEvent(); |
56
|
|
|
|
57
|
|
|
// Db transaction |
58
|
|
|
$transaction = RecordHelper::beginTransaction(); |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
|
62
|
|
|
// The 'before' event |
63
|
|
|
if (!$model->beforeDelete($event)) { |
64
|
|
|
|
65
|
|
|
$transaction->rollBack(); |
66
|
|
|
|
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Get record |
71
|
|
|
$record = $this->getRecordById($model->id); |
72
|
|
|
|
73
|
|
|
// Insert record |
74
|
|
|
if (!$record->delete()) { |
|
|
|
|
75
|
|
|
|
76
|
|
|
// Transfer errors to model |
77
|
|
|
$model->addErrors($record->getErrors()); |
78
|
|
|
|
79
|
|
|
// Roll back db transaction |
80
|
|
|
$transaction->rollBack(); |
81
|
|
|
|
82
|
|
|
return false; |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// The 'after' event |
87
|
|
|
if (!$model->afterDelete($event)) { |
88
|
|
|
|
89
|
|
|
// Roll back db transaction |
90
|
|
|
$transaction->rollBack(); |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} catch (\Exception $e) { |
97
|
|
|
|
98
|
|
|
// Roll back all db actions (fail) |
99
|
|
|
$transaction->rollback(); |
100
|
|
|
|
101
|
|
|
throw $e; |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$transaction->commit(); |
106
|
|
|
|
107
|
|
|
// an 'afterDelete' event |
108
|
|
|
$this->afterDelete($model); |
109
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param Model $model |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
protected function beforeDelete(Model $model): bool |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Model $model |
125
|
|
|
*/ |
126
|
|
|
protected function afterDelete(Model $model) |
127
|
|
|
{ |
128
|
|
|
|
129
|
|
|
Craft::info(sprintf( |
130
|
|
|
"Model '%s' with ID '%s' was deleted successfully.", |
131
|
|
|
(string) get_class($model), |
132
|
|
|
(string) $model->id |
133
|
|
|
), __METHOD__); |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: