1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Spark |
5
|
|
|
* @author Flipbox Factory <[email protected]> |
6
|
|
|
* @copyright 2010-2016 Flipbox Digital Limited |
7
|
|
|
* @license https://github.com/FlipboxFactory/Craft3-Spark/blob/master/LICENSE |
8
|
|
|
* @link https://github.com/FlipboxFactory/Craft3-Spark |
9
|
|
|
* @since Class available since Release 1.0.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace flipbox\spark\services\traits; |
13
|
|
|
|
14
|
|
|
use flipbox\spark\helpers\RecordHelper; |
15
|
|
|
use flipbox\spark\models\Model; |
16
|
|
|
use flipbox\spark\records\Record; |
17
|
|
|
use yii\base\ModelEvent; |
18
|
|
|
|
19
|
|
|
trait ModelDelete |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/******************************************* |
23
|
|
|
* ABSTRACTS |
24
|
|
|
*******************************************/ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param int $id |
28
|
|
|
* @param string|null $toScenario |
29
|
|
|
* @return Record |
30
|
|
|
*/ |
31
|
|
|
abstract public function getRecordById(int $id, string $toScenario = null): Record; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/******************************************* |
35
|
|
|
* EVENT |
36
|
|
|
*******************************************/ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Model $model |
40
|
|
|
* @return ModelEvent |
41
|
|
|
*/ |
42
|
|
|
protected function createEvent(Model $model): ModelEvent |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
return new ModelEvent(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/******************************************* |
48
|
|
|
* DELETE |
49
|
|
|
*******************************************/ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Model $model |
53
|
|
|
* @return bool |
54
|
|
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
|
|
public function delete(Model $model): bool |
57
|
|
|
{ |
58
|
|
|
|
59
|
|
|
// The event to trigger |
60
|
|
|
$event = $this->createEvent(); |
|
|
|
|
61
|
|
|
|
62
|
|
|
// Db transaction |
63
|
|
|
$transaction = RecordHelper::beginTransaction(); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
|
67
|
|
|
// The 'before' event |
68
|
|
|
if (!$model->beforeDelete($event)) { |
69
|
|
|
|
70
|
|
|
$transaction->rollBack(); |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Get record |
76
|
|
|
$record = $this->getRecordById($model->id); |
77
|
|
|
|
78
|
|
|
// Insert record |
79
|
|
|
if (!$record->delete()) { |
|
|
|
|
80
|
|
|
|
81
|
|
|
// Transfer errors to model |
82
|
|
|
$model->addErrors($record->getErrors()); |
83
|
|
|
|
84
|
|
|
// Roll back db transaction |
85
|
|
|
$transaction->rollBack(); |
86
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// The 'after' event |
92
|
|
|
if (!$model->afterDelete($event)) { |
93
|
|
|
|
94
|
|
|
// Roll back db transaction |
95
|
|
|
$transaction->rollBack(); |
96
|
|
|
|
97
|
|
|
return false; |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} catch (\Exception $e) { |
102
|
|
|
|
103
|
|
|
// Roll back all db actions (fail) |
104
|
|
|
$transaction->rollback(); |
105
|
|
|
|
106
|
|
|
throw $e; |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$transaction->commit(); |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.