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 craft\events\ModelEvent; |
15
|
|
|
use flipbox\spark\helpers\RecordHelper; |
16
|
|
|
use flipbox\spark\models\Model; |
17
|
|
|
use flipbox\spark\records\Record; |
18
|
|
|
|
19
|
|
|
trait ModelSave |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/******************************************* |
23
|
|
|
* ABSTRACTS |
24
|
|
|
*******************************************/ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Model $model |
28
|
|
|
* @param bool $mirrorScenario |
29
|
|
|
* @return Record |
30
|
|
|
*/ |
31
|
|
|
abstract public function toRecord(Model $model, bool $mirrorScenario = true): 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
|
|
|
'isNew' => null !== $model->id |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/******************************************* |
50
|
|
|
* SAVE |
51
|
|
|
*******************************************/ |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Model $model |
55
|
|
|
* @param null $attributes |
56
|
|
|
* @param bool $mirrorScenario |
57
|
|
|
* @return bool |
58
|
|
|
* @throws \Exception |
59
|
|
|
*/ |
60
|
|
|
public function save(Model $model, $attributes = null, bool $mirrorScenario = true) |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
// Create event |
64
|
|
|
$event = $this->createEvent($model); |
65
|
|
|
|
66
|
|
|
// Db transaction |
67
|
|
|
$transaction = RecordHelper::beginTransaction(); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
|
71
|
|
|
// The 'before' event |
72
|
|
|
if (!$model->beforeSave($event)) { |
73
|
|
|
|
74
|
|
|
$transaction->rollBack(); |
75
|
|
|
|
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$record = $this->toRecord($model, $mirrorScenario); |
80
|
|
|
|
81
|
|
|
// Validate |
82
|
|
|
if (!$record->validate($attributes)) { |
83
|
|
|
|
84
|
|
|
$model->addErrors($record->getErrors()); |
85
|
|
|
|
86
|
|
|
$transaction->rollBack(); |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Insert record |
93
|
|
|
if (!$record->save($attributes)) { |
|
|
|
|
94
|
|
|
|
95
|
|
|
// Transfer errors to model |
96
|
|
|
$model->addErrors($record->getErrors()); |
97
|
|
|
|
98
|
|
|
$transaction->rollBack(); |
99
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Transfer record to model |
105
|
|
|
if ($event->isNew) { |
106
|
|
|
$model->id = $record->id; |
107
|
|
|
$model->dateCreated = $record->dateCreated; |
108
|
|
|
$model->uid = $record->uid; |
109
|
|
|
} |
110
|
|
|
$model->dateUpdated = $record->dateUpdated; |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
// The 'after' event |
114
|
|
|
if (!$model->afterSave($event)) { |
115
|
|
|
|
116
|
|
|
$transaction->rollBack(); |
117
|
|
|
|
118
|
|
|
return false; |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} catch (\Exception $e) { |
123
|
|
|
|
124
|
|
|
$transaction->rollBack(); |
125
|
|
|
|
126
|
|
|
throw $e; |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$transaction->commit(); |
131
|
|
|
|
132
|
|
|
return true; |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: