1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
7
|
|
|
|
8
|
|
|
trait Create |
9
|
|
|
{ |
10
|
|
|
/* |
11
|
|
|
|-------------------------------------------------------------------------- |
12
|
|
|
| CREATE |
13
|
|
|
|-------------------------------------------------------------------------- |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Insert a row in the database. |
18
|
|
|
* |
19
|
|
|
* @param array $data All input values to be inserted. |
20
|
|
|
* |
21
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
22
|
|
|
*/ |
23
|
4 |
|
public function create($data) |
24
|
|
|
{ |
25
|
4 |
|
$data = $this->decodeJsonCastedAttributes($data, 'create'); |
|
|
|
|
26
|
4 |
|
$data = $this->compactFakeFields($data, 'create'); |
|
|
|
|
27
|
|
|
|
28
|
|
|
// omit the n-n relationships when updating the eloquent item |
29
|
4 |
|
$nn_relationships = array_pluck($this->getRelationFieldsWithPivot('create'), 'name'); |
|
|
|
|
30
|
4 |
|
$item = $this->model->create(array_except($data, $nn_relationships)); |
|
|
|
|
31
|
|
|
|
32
|
|
|
// if there are any relationships available, also sync those |
33
|
4 |
|
$this->createRelations($item, $data); |
34
|
|
|
|
35
|
4 |
|
return $item; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get all fields needed for the ADD NEW ENTRY form. |
40
|
|
|
* |
41
|
|
|
* @return array The fields with attributes and fake attributes. |
42
|
|
|
*/ |
43
|
25 |
|
public function getCreateFields() |
44
|
|
|
{ |
45
|
25 |
|
return $this->create_fields; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get all fields with relation set (model key set on field). |
50
|
|
|
* |
51
|
|
|
* @param string $form create/update/both |
52
|
|
|
* |
53
|
|
|
* @return array The fields with model key set. |
54
|
|
|
*/ |
55
|
15 |
|
public function getRelationFields($form = 'create') |
56
|
|
|
{ |
57
|
15 |
|
if ($form == 'create') { |
58
|
13 |
|
$fields = $this->create_fields; |
59
|
|
|
} else { |
60
|
2 |
|
$fields = $this->update_fields; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
15 |
|
$relationFields = []; |
64
|
|
|
|
65
|
15 |
|
foreach ($fields as $field) { |
66
|
14 |
|
if (isset($field['model'])) { |
67
|
8 |
|
array_push($relationFields, $field); |
68
|
|
|
} |
69
|
|
|
|
70
|
14 |
|
if (isset($field['subfields']) && |
71
|
14 |
|
is_array($field['subfields']) && |
72
|
14 |
|
count($field['subfields'])) { |
73
|
|
|
foreach ($field['subfields'] as $subfield) { |
74
|
|
|
array_push($relationFields, $subfield); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
15 |
|
return $relationFields; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get all fields with n-n relation set (pivot table is true). |
84
|
|
|
* |
85
|
|
|
* @param string $form create/update/both |
86
|
|
|
* |
87
|
|
|
* @return array The fields with n-n relationships. |
88
|
|
|
*/ |
89
|
7 |
|
public function getRelationFieldsWithPivot($form = 'create') |
90
|
|
|
{ |
91
|
7 |
|
$all_relation_fields = $this->getRelationFields($form); |
92
|
|
|
|
93
|
|
|
return array_where($all_relation_fields, function ($value, $key) { |
|
|
|
|
94
|
3 |
|
return isset($value['pivot']) && $value['pivot']; |
95
|
7 |
|
}); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create the relations for the current model. |
100
|
|
|
* |
101
|
|
|
* @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
102
|
|
|
* @param array $data The form data. |
103
|
|
|
* @param string $form Optional form type. Can be either 'create', 'update' or 'both'. Default is 'create'. |
104
|
|
|
*/ |
105
|
5 |
|
public function createRelations($item, $data, $form = 'create') |
106
|
|
|
{ |
107
|
5 |
|
$this->syncPivot($item, $data, $form); |
108
|
5 |
|
$this->createOneToOneRelations($item, $data, $form); |
109
|
5 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Sync the declared many-to-many associations through the pivot field. |
113
|
|
|
* |
114
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
115
|
|
|
* @param array $data The form data. |
116
|
|
|
* @param string $form Optional form type. Can be either 'create', 'update' or 'both'. Default is 'create'. |
117
|
|
|
*/ |
118
|
8 |
|
public function syncPivot($model, $data, $form = 'create') |
119
|
|
|
{ |
120
|
8 |
|
$fields_with_relationships = $this->getRelationFields($form); |
121
|
|
|
|
122
|
8 |
|
foreach ($fields_with_relationships as $key => $field) { |
123
|
4 |
|
if (isset($field['pivot']) && $field['pivot']) { |
124
|
3 |
|
$values = isset($data[$field['name']]) ? $data[$field['name']] : []; |
125
|
|
|
|
126
|
3 |
|
$relation_data = []; |
127
|
3 |
|
foreach ($values as $pivot_id) { |
128
|
3 |
|
$pivot_data = []; |
129
|
|
|
|
130
|
3 |
|
if (isset($field['pivotFields'])) { |
131
|
|
|
foreach ($field['pivotFields'] as $pivot_field_name) { |
132
|
|
|
$pivot_data[$pivot_field_name] = $data[$pivot_field_name][$pivot_id]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
3 |
|
$relation_data[$pivot_id] = $pivot_data; |
136
|
|
|
} |
137
|
|
|
|
138
|
3 |
|
$model->{$field['name']}()->sync($relation_data); |
139
|
|
|
} |
140
|
|
|
|
141
|
3 |
|
if (isset($field['morph']) && $field['morph'] && isset($data[$field['name']])) { |
142
|
|
|
$values = $data[$field['name']]; |
143
|
|
|
$model->{$field['name']}()->sync($values); |
144
|
|
|
} |
145
|
|
|
} |
146
|
7 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Create any existing one to one relations for the current model from the form data. |
150
|
|
|
* |
151
|
|
|
* @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
152
|
|
|
* @param array $data The form data. |
153
|
|
|
* @param string $form Optional form type. Can be either 'create', 'update' or 'both'. Default is 'create'. |
154
|
|
|
*/ |
155
|
5 |
|
private function createOneToOneRelations($item, $data, $form = 'create') |
156
|
|
|
{ |
157
|
5 |
|
$relationData = $this->getRelationDataFromFormData($data, $form); |
158
|
5 |
|
$this->createRelationsForItem($item, $relationData); |
159
|
5 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Create any existing one to one relations for the current model from the relation data. |
163
|
|
|
* |
164
|
|
|
* @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
165
|
|
|
* @param array $formattedData The form data. |
166
|
|
|
* |
167
|
|
|
* @return bool|null |
|
|
|
|
168
|
|
|
*/ |
169
|
5 |
|
private function createRelationsForItem($item, $formattedData) |
170
|
|
|
{ |
171
|
5 |
|
if (! isset($formattedData['relations'])) { |
172
|
4 |
|
return false; |
173
|
|
|
} |
174
|
|
|
|
175
|
1 |
|
foreach ($formattedData['relations'] as $relationMethod => $relationData) { |
176
|
1 |
|
$model = $relationData['model']; |
177
|
1 |
|
$relation = $item->{$relationMethod}(); |
178
|
|
|
|
179
|
1 |
|
if ($relation instanceof BelongsTo) { |
180
|
1 |
|
$modelInstance = $model::find($relationData['values'])->first(); |
181
|
1 |
|
if ($modelInstance != null) { |
182
|
1 |
|
$relation->associate($modelInstance)->save(); |
183
|
|
|
} else { |
184
|
1 |
|
$relation->dissociate()->save(); |
185
|
|
|
} |
186
|
|
|
} elseif ($relation instanceof HasOne) { |
187
|
|
|
if ($item->{$relationMethod} != null) { |
188
|
|
|
$item->{$relationMethod}->update($relationData['values']); |
189
|
|
|
$modelInstance = $item->{$relationMethod}; |
190
|
|
|
} else { |
191
|
|
|
$relationModel = new $model(); |
192
|
|
|
$modelInstance = $relationModel->create($relationData['values']); |
193
|
|
|
$relation->save($modelInstance); |
194
|
|
|
} |
195
|
|
|
} else { |
196
|
|
|
$relationModel = new $model(); |
197
|
|
|
$modelInstance = $relationModel->create($relationData['values']); |
198
|
|
|
$relation->save($modelInstance); |
199
|
|
|
} |
200
|
|
|
|
201
|
1 |
|
if (isset($relationData['relations'])) { |
202
|
|
|
$this->createRelationsForItem($modelInstance, ['relations' => $relationData['relations']]); |
203
|
|
|
} |
204
|
|
|
} |
205
|
1 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get a relation data array from the form data. |
209
|
|
|
* For each relation defined in the fields through the entity attribute, set the model, the parent model and the |
210
|
|
|
* attribute values. For relations defined with the "dot" notations, this will be used to calculate the depth in the |
211
|
|
|
* final array (@see \Illuminate\Support\Arr::set() for more). |
212
|
|
|
* |
213
|
|
|
* @param array $data The form data. |
214
|
|
|
* @param string $form Optional form type. Can be either 'create', 'update' or 'both'. Default is 'create'. |
215
|
|
|
* |
216
|
|
|
* @return array The formatted relation data. |
217
|
|
|
*/ |
218
|
5 |
|
private function getRelationDataFromFormData($data, $form = 'create') |
219
|
|
|
{ |
220
|
5 |
|
$relationFields = $this->getRelationFields($form); |
221
|
|
|
|
222
|
5 |
|
$relationData = []; |
223
|
5 |
|
foreach ($relationFields as $relationField) { |
224
|
2 |
|
$attributeKey = $relationField['name']; |
225
|
2 |
|
if (array_key_exists($attributeKey, $data) && empty($relationField['pivot'])) { |
226
|
1 |
|
$key = implode('.relations.', explode('.', $relationField['entity'])); |
227
|
1 |
|
$fieldData = array_get($relationData, 'relations.'.$key, []); |
|
|
|
|
228
|
|
|
|
229
|
1 |
|
if (! array_key_exists('model', $fieldData)) { |
230
|
1 |
|
$fieldData['model'] = $relationField['model']; |
231
|
|
|
} |
232
|
|
|
|
233
|
1 |
|
if (! array_key_exists('parent', $fieldData)) { |
234
|
1 |
|
$fieldData['parent'] = $this->getRelationModel($relationField['entity'], -1); |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
1 |
|
$fieldData['values'][$attributeKey] = $data[$attributeKey]; |
238
|
|
|
|
239
|
1 |
|
array_set($relationData, 'relations.'.$key, $fieldData); |
|
|
|
|
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
5 |
|
return $relationData; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.