1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Traits\ModelAdmin; |
4
|
|
|
|
5
|
|
|
use LaravelFlare\Flare\Events\ModelSave; |
6
|
|
|
use LaravelFlare\Flare\Events\AfterSave; |
7
|
|
|
use LaravelFlare\Flare\Events\BeforeSave; |
8
|
|
|
use LaravelFlare\Flare\Exceptions\ModelAdminWriteableException as WriteableException; |
9
|
|
|
|
10
|
|
|
trait ModelWriting |
11
|
|
|
{ |
12
|
|
|
use ModelCreating, ModelEditting, ModelDeleting; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Used by beforeSave() to ensure child classes call parent::beforeSave(). |
16
|
|
|
* |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
protected $brokenBeforeSave = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Used by afterSave() to ensure child classes call parent::afterSave(). |
23
|
|
|
* |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
protected $brokenAfterSave = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Relations to Update during Save and |
30
|
|
|
* the appropriate method to fire the update with. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $doSaveRelations = ['BelongsTo' => 'associate']; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Relations to Update after Save and |
38
|
|
|
* the appropriate method to fire the update with. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $afterSaveRelations = ['BelongsToMany' => 'sync']; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Trait Requires hasSetMutator method. |
46
|
|
|
* |
47
|
|
|
* @param string $key |
48
|
|
|
* |
49
|
|
|
* @return |
50
|
|
|
*/ |
51
|
|
|
abstract public function hasSetMutator($key); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Trait Requires setAttribute method. |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* @param string $value |
58
|
|
|
* |
59
|
|
|
* @return |
60
|
|
|
*/ |
61
|
|
|
abstract public function setAttribute($key, $value); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Trait Requires Find Method (usually provided by ModelQuerying). |
65
|
|
|
* |
66
|
|
|
* @param int $modelitem_id |
67
|
|
|
* |
68
|
|
|
* @return |
69
|
|
|
*/ |
70
|
|
|
abstract protected function find($modelitem_id); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Method fired before the Save action is undertaken. |
74
|
|
|
* |
75
|
|
|
* @return |
76
|
|
|
*/ |
77
|
|
|
protected function beforeSave() |
78
|
|
|
{ |
79
|
|
|
$this->brokenBeforeSave = false; |
80
|
|
|
|
81
|
|
|
event(new BeforeSave($this)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Save Action. |
86
|
|
|
* |
87
|
|
|
* Fires off beforeSave(), doSave() and afterSave() |
88
|
|
|
* |
89
|
|
|
* @return |
90
|
|
|
*/ |
91
|
|
|
public function save() |
92
|
|
|
{ |
93
|
|
|
$this->brokenBeforeSave = true; |
94
|
|
|
$this->beforeSave(); |
95
|
|
|
if ($this->brokenBeforeSave) { |
96
|
|
|
throw new WriteableException('ModelAdmin has a broken beforeSave method. Make sure you call parent::beforeSave() on all instances of beforeSave()', 1); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->doSave(); |
100
|
|
|
|
101
|
|
|
$this->brokenAfterSave = true; |
102
|
|
|
$this->afterSave(); |
103
|
|
|
if ($this->brokenAfterSave) { |
104
|
|
|
throw new WriteableException('ModelAdmin has a broken afterSave method. Make sure you call parent::afterSave() on all instances of afterSave()', 1); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* The actual Save action, which does all of hte pre-processing |
110
|
|
|
* required before we are able to perform the save() function. |
111
|
|
|
* |
112
|
|
|
* @return |
113
|
|
|
*/ |
114
|
|
|
private function doSave() |
115
|
|
|
{ |
116
|
|
|
foreach (\Request::only(array_keys($this->fields)) as $key => $value) { |
|
|
|
|
117
|
|
|
if (!\Schema::hasColumn($this->model->getTable(), $key)) { |
118
|
|
|
continue; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($this->hasSetMutator($key)) { |
122
|
|
|
$this->setAttribute($key, $value); |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful |
127
|
|
View Code Duplication |
if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) { |
|
|
|
|
128
|
|
|
$this->saveRelation('doSave', $key, $value); |
129
|
|
|
continue; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$this->model->setAttribute($key, $value); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->model->save(); |
136
|
|
|
|
137
|
|
|
event(new ModelSave($this)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Method fired after the Save action is complete. |
142
|
|
|
* |
143
|
|
|
* @return |
144
|
|
|
*/ |
145
|
|
|
protected function afterSave() |
146
|
|
|
{ |
147
|
|
|
$this->brokenAfterSave = false; |
148
|
|
|
|
149
|
|
|
foreach (\Request::except('_token') as $key => $value) { |
150
|
|
|
|
151
|
|
|
// Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful |
152
|
|
View Code Duplication |
if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) { |
|
|
|
|
153
|
|
|
$this->saveRelation('afterSave', $key, $value); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
event(new AfterSave($this)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Method fired to Save Relations. |
162
|
|
|
* |
163
|
|
|
* @param string $action The current action (either doSave or afterSave) |
164
|
|
|
* @param string $key |
165
|
|
|
* @param string $value |
166
|
|
|
* |
167
|
|
|
* @return |
168
|
|
|
*/ |
169
|
|
|
private function saveRelation($action, $key, $value) |
170
|
|
|
{ |
171
|
|
|
// Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful |
172
|
|
|
if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) { |
173
|
|
|
foreach ($this->{$action.'Relations'} as $relationship => $method) { |
174
|
|
|
if (is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\\'.$relationship)) { |
175
|
|
|
$this->model->$key()->$method($value); |
176
|
|
|
|
177
|
|
|
return; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: