1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Traits; |
4
|
|
|
|
5
|
|
|
use CodexShaper\DBM\Facades\Manager as DBM; |
6
|
|
|
|
7
|
|
|
trait RecordRelationship |
8
|
|
|
{ |
9
|
|
|
protected $has_one; |
10
|
|
|
protected $has_many; |
11
|
|
|
protected $belongs_to_many; |
12
|
|
|
protected $belongs_to; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Prepare Relationship data to view. |
16
|
|
|
* |
17
|
|
|
* @param mixed $records |
18
|
|
|
* @param \Illuminate\Support\Collection $browseFields |
19
|
|
|
* @param \CodexShaper\DBM\Models\DBM_Object|\CodexShaper\DBM\Models\DBM_MongoObject $object |
20
|
|
|
* |
21
|
|
|
* @return mixed |
22
|
|
|
*/ |
23
|
|
|
public function prepareRelationshipData($records, $browseFields, $object) |
24
|
|
|
{ |
25
|
|
|
foreach ($records as $item => $record) { |
26
|
|
|
foreach ($browseFields as $field) { |
27
|
|
|
if ($field->type == 'relationship') { |
28
|
|
|
$relationship = $field->settings; |
29
|
|
|
|
30
|
|
|
$findColumn = $object->details['findColumn']; |
31
|
|
|
|
32
|
|
|
$localModel = $relationship['localModel']; |
33
|
|
|
$localKey = $relationship['localKey']; |
34
|
|
|
$foreignModel = $relationship['foreignModel']; |
35
|
|
|
$foreignKey = $relationship['foreignKey']; |
36
|
|
|
$relationshipType = $relationship['relationType']; |
37
|
|
|
$displayLabel = $relationship['displayLabel']; |
38
|
|
|
|
39
|
|
|
if ($relationshipType == 'belongsTo') { |
40
|
|
|
$localObject = $localModel::where($findColumn, $record->{$findColumn})->first(); |
41
|
|
|
|
42
|
|
|
$datas = DBM::Object()->setCommonRelation($localObject, $foreignModel, $foreignKey, $localKey)->belongs_to; |
|
|
|
|
43
|
|
|
|
44
|
|
|
$record->{$field->name} = $datas; |
45
|
|
|
$field->displayLabel = $displayLabel; |
46
|
|
|
$field->localKey = $localKey; |
47
|
|
|
$field->foreignKey = $foreignKey; |
48
|
|
|
$field->relationshipType = $relationshipType; |
49
|
|
|
} elseif ($relationshipType == 'hasOne') { |
50
|
|
|
$localObject = $localModel::where($findColumn, $record->{$findColumn})->first(); |
51
|
|
|
$datas = DBM::Object()->setCommonRelation($localObject, $foreignModel, $foreignKey, $localKey)->has_one; |
|
|
|
|
52
|
|
|
|
53
|
|
|
$record->{$field->name} = $datas; |
54
|
|
|
$field->displayLabel = $displayLabel; |
55
|
|
|
$field->localKey = $localKey; |
56
|
|
|
$field->foreignKey = $foreignKey; |
57
|
|
|
$field->relationshipType = $relationshipType; |
58
|
|
|
} elseif ($relationshipType == 'hasMany') { |
59
|
|
|
$localObject = $localModel::where($findColumn, $record->{$findColumn})->first(); |
60
|
|
|
$datas = DBM::Object()->setCommonRelation($localObject, $foreignModel, $foreignKey, $localKey)->has_many; |
|
|
|
|
61
|
|
|
|
62
|
|
|
$record->{$field->name} = $datas; |
63
|
|
|
$field->displayLabel = $displayLabel; |
64
|
|
|
$field->localKey = $localKey; |
65
|
|
|
$field->foreignKey = $foreignKey; |
66
|
|
|
$field->relationshipType = $relationshipType; |
67
|
|
|
} elseif ($relationshipType == 'belongsToMany') { |
68
|
|
|
$pivotTable = $relationship['pivotTable']; |
69
|
|
|
$parentPivotKey = $relationship['parentPivotKey']; |
70
|
|
|
$relatedPivotKey = $relationship['relatedPivotKey']; |
71
|
|
|
|
72
|
|
|
$localObject = $localModel::where($findColumn, $record->{$findColumn})->first(); |
73
|
|
|
|
74
|
|
|
$datas = DBM::Object()->setManyToManyRelation($localObject, $foreignModel, $pivotTable, $parentPivotKey, $relatedPivotKey)->belongs_to_many; |
|
|
|
|
75
|
|
|
|
76
|
|
|
$record->{$field->name} = $datas; |
77
|
|
|
$field->displayLabel = $displayLabel; |
78
|
|
|
$field->localKey = $localKey; |
79
|
|
|
$field->foreignKey = $foreignKey; |
80
|
|
|
$field->relationshipType = $relationshipType; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $records; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Create new Relationship. |
91
|
|
|
* |
92
|
|
|
* @param \Illuminate\Support\Collection $fields |
93
|
|
|
* @param object $columns |
94
|
|
|
* @param \CodexShaper\DBM\Models\DBM_Object|\CodexShaper\DBM\Models\DBM_MongoObject $object |
95
|
|
|
* @param object $table |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function storeRelationshipData($fields, $columns, $object, $table) |
100
|
|
|
{ |
101
|
|
|
foreach ($fields as $field) { |
102
|
|
|
if (isset($field->relationship) && $field->relationship->relationType == 'belongsToMany') { |
103
|
|
|
$relationship = $field->relationship; |
104
|
|
|
|
105
|
|
|
$localModel = $relationship->localModel; |
106
|
|
|
$localTable = $relationship->localTable; |
107
|
|
|
$foreignModel = $relationship->foreignModel; |
108
|
|
|
$pivotTable = $relationship->pivotTable; |
109
|
|
|
$parentPivotKey = $relationship->parentPivotKey; |
110
|
|
|
$relatedPivotKey = $relationship->relatedPivotKey; |
111
|
|
|
|
112
|
|
|
$findColumn = $object->details['findColumn']; |
113
|
|
|
|
114
|
|
|
$localObject = DBM::model($localModel, $localTable)::where($findColumn, $table->{$findColumn})->first(); |
115
|
|
|
|
116
|
|
|
DBM::Object() |
117
|
|
|
->setManyToManyRelation( |
118
|
|
|
$localObject, |
119
|
|
|
$foreignModel, |
120
|
|
|
$pivotTable, |
121
|
|
|
$parentPivotKey, |
122
|
|
|
$relatedPivotKey |
123
|
|
|
) |
124
|
|
|
->belongs_to_many() |
125
|
|
|
->attach($columns->{$relatedPivotKey}); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Update Relationship. |
132
|
|
|
* |
133
|
|
|
* @param \Illuminate\Support\Collection $fields |
134
|
|
|
* @param object $columns |
135
|
|
|
* @param \CodexShaper\DBM\Models\DBM_Object|\CodexShaper\DBM\Models\DBM_MongoObject $object |
136
|
|
|
* @param object $table |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public function updateRelationshipData($fields, $columns, $object, $table) |
141
|
|
|
{ |
142
|
|
|
foreach ($fields as $field) { |
143
|
|
|
if (isset($field->relationship)) { |
144
|
|
|
$relationship = $field->relationship; |
145
|
|
|
|
146
|
|
|
$localModel = $relationship->localModel; |
147
|
|
|
$localTable = $relationship->localTable; |
148
|
|
|
$foreignModel = $relationship->foreignModel; |
149
|
|
|
|
150
|
|
|
if ($field->relationship->relationType == 'belongsToMany') { |
151
|
|
|
$pivotTable = $relationship->pivotTable; |
152
|
|
|
$parentPivotKey = $relationship->parentPivotKey; |
153
|
|
|
$relatedPivotKey = $relationship->relatedPivotKey; |
154
|
|
|
$findColumn = $object->details['findColumn']; |
155
|
|
|
|
156
|
|
|
$localObject = DBM::model($localModel, $localTable)->where($findColumn, $table->{$findColumn})->first(); |
157
|
|
|
|
158
|
|
|
DBM::Object() |
159
|
|
|
->setManyToManyRelation( |
160
|
|
|
$localObject, |
161
|
|
|
$foreignModel, |
162
|
|
|
$pivotTable, |
163
|
|
|
$parentPivotKey, |
164
|
|
|
$relatedPivotKey |
165
|
|
|
) |
166
|
|
|
->belongs_to_many() |
167
|
|
|
->sync($columns->{$relatedPivotKey}); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Remove Relationship. |
175
|
|
|
* |
176
|
|
|
* @param object $field |
177
|
|
|
* @param \CodexShaper\DBM\Models\DBM_Object|\CodexShaper\DBM\Models\DBM_MongoObject $object |
178
|
|
|
* @param object $table |
179
|
|
|
* |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
|
|
public function removeRelationshipData($field, $object, $table) |
183
|
|
|
{ |
184
|
|
|
if ($field->type == 'relationship') { |
185
|
|
|
$relationship = $field->settings; |
186
|
|
|
|
187
|
|
|
$localModel = $relationship->localModel; |
188
|
|
|
$foreignModel = $relationship->foreignModel; |
189
|
|
|
|
190
|
|
|
$findColumn = $object->details['findColumn']; |
191
|
|
|
|
192
|
|
|
$localObject = $localModel::where($findColumn, $table->{$findColumn})->first(); |
193
|
|
|
|
194
|
|
|
if ($relationship->relationType == 'belongsToMany') { |
195
|
|
|
$pivotTable = $relationship->pivotTable; |
196
|
|
|
$parentPivotKey = $relationship->parentPivotKey; |
197
|
|
|
$relatedPivotKey = $relationship->relatedPivotKey; |
198
|
|
|
|
199
|
|
|
DBM::Object() |
200
|
|
|
->setManyToManyRelation( |
201
|
|
|
$localObject, |
202
|
|
|
$foreignModel, |
203
|
|
|
$pivotTable, |
204
|
|
|
$parentPivotKey, |
205
|
|
|
$relatedPivotKey |
206
|
|
|
) |
207
|
|
|
->belongs_to_many() |
208
|
|
|
->detach(); |
209
|
|
|
} elseif ($relationship->relationType == 'hasMany') { |
210
|
|
|
$foreignKey = $relationship->foreignKey; |
211
|
|
|
$localKey = $relationship->localKey; |
212
|
|
|
|
213
|
|
|
DBM::Object() |
214
|
|
|
->setCommonRelation( |
215
|
|
|
$localObject, |
216
|
|
|
$foreignModel, |
217
|
|
|
$foreignKey, |
218
|
|
|
$localKey) |
219
|
|
|
->has_many() |
220
|
|
|
->delete(); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.