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
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
trait ModelDelete |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/******************************************* |
25
|
|
|
* ABSTRACTS |
26
|
|
|
*******************************************/ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Model $model |
30
|
|
|
* @return Record |
31
|
|
|
*/ |
32
|
|
|
abstract public function getRecordByModel(Model $model): Record; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/******************************************* |
36
|
|
|
* DELETE |
37
|
|
|
*******************************************/ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Model $model |
41
|
|
|
* @return bool |
42
|
|
|
* @throws \Exception |
43
|
|
|
*/ |
44
|
|
|
public function delete(Model $model): bool |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
// a 'beforeSave' event |
48
|
|
|
if (!$this->beforeDelete($model)) { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// The event to trigger |
53
|
|
|
$event = new ModelEvent(); |
54
|
|
|
|
55
|
|
|
// Db transaction |
56
|
|
|
$transaction = RecordHelper::beginTransaction(); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
// The 'before' event |
60
|
|
|
if (!$model->beforeDelete($event)) { |
61
|
|
|
$transaction->rollBack(); |
62
|
|
|
|
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Get record |
67
|
|
|
/** @var Record $record */ |
68
|
|
|
$record = $this->getRecordByModel($model); |
69
|
|
|
|
70
|
|
|
// Insert record |
71
|
|
|
if (!$record->delete()) { |
|
|
|
|
72
|
|
|
// Transfer errors to model |
73
|
|
|
$model->addErrors($record->getErrors()); |
74
|
|
|
|
75
|
|
|
// Roll back db transaction |
76
|
|
|
$transaction->rollBack(); |
77
|
|
|
|
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// The 'after' event |
82
|
|
|
if (!$model->afterDelete($event)) { |
83
|
|
|
// Roll back db transaction |
84
|
|
|
$transaction->rollBack(); |
85
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
} catch (\Exception $e) { |
89
|
|
|
// Roll back all db actions (fail) |
90
|
|
|
$transaction->rollback(); |
91
|
|
|
|
92
|
|
|
throw $e; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$transaction->commit(); |
96
|
|
|
|
97
|
|
|
// an 'afterDelete' event |
98
|
|
|
$this->afterDelete($model); |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param Model $model |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
protected function beforeDelete(Model $model): bool |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Model $model |
114
|
|
|
*/ |
115
|
|
|
protected function afterDelete(Model $model) |
116
|
|
|
{ |
117
|
|
|
|
118
|
|
|
Craft::info(sprintf( |
119
|
|
|
"Model '%s' was deleted successfully.", |
120
|
|
|
(string)get_class($model) |
121
|
|
|
), __METHOD__); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: