Completed
Push — master ( acc6c6...cef23c )
by CodexShaper
04:44
created

RecordRelationship::storeRelationshipData()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 20
c 1
b 0
f 0
nc 3
nop 4
dl 0
loc 29
ccs 0
cts 23
cp 0
crap 20
rs 9.6
1
<?php
2
3
namespace CodexShaper\DBM\Traits;
4
5
use CodexShaper\DBM\Database\Drivers\MongoDB\Type;
6
use CodexShaper\DBM\Facades\Manager as DBM;
7
8
trait RecordRelationship
9
{
10
    public function prepareRelationshipData($records, $browseFields, $object)
11
    {
12
        foreach ($records as $item => $record) {
13
14
            foreach ($browseFields as $field) {
15
16
                if ($field->type == 'relationship') {
17
18
                    $relationship = $field->settings;
19
20
                    $findColumn = $object->details['findColumn'];
21
22
                    $localModel       = $relationship['localModel'];
23
                    $localKey         = $relationship['localKey'];
24
                    $foreignModel     = $relationship['foreignModel'];
25
                    $foreignKey       = $relationship['foreignKey'];
26
                    $relationshipType = $relationship['relationType'];
27
                    $displayLabel     = $relationship['displayLabel'];
28
29
                    if ($relationshipType == 'belongsTo') {
30
31
                        $localObject = $localModel::where($findColumn, $record->{$findColumn})->first();
32
33
                        $datas = DBM::Object()->setCommonRelation($localObject, $foreignModel, $foreignKey, $localKey)->belongs_to;
0 ignored issues
show
Bug introduced by
The property belongs_to does not seem to exist on CodexShaper\DBM\Models\DBM_Object. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
34
35
                        $record->{$field->name}  = $datas;
36
                        $field->displayLabel     = $displayLabel;
37
                        $field->localKey         = $localKey;
38
                        $field->foreignKey       = $foreignKey;
39
                        $field->relationshipType = $relationshipType;
40
41
                    } else if ($relationshipType == 'hasMany') {
42
43
                        $localObject = $localModel::where($findColumn, $record->{$findColumn})->first();
44
                        $datas       = DBM::Object()->setCommonRelation($localObject, $foreignModel, $foreignKey, $localKey)->has_many;
0 ignored issues
show
Bug introduced by
The property has_many does not seem to exist on CodexShaper\DBM\Models\DBM_Object. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
45
46
                        $record->{$field->name}  = $datas;
47
                        $field->displayLabel     = $displayLabel;
48
                        $field->localKey         = $localKey;
49
                        $field->foreignKey       = $foreignKey;
50
                        $field->relationshipType = $relationshipType;
51
52
                    } else if ($relationshipType == 'belongsToMany') {
53
54
                        $pivotTable      = $relationship['pivotTable'];
55
                        $parentPivotKey  = $relationship['parentPivotKey'];
56
                        $relatedPivotKey = $relationship['relatedPivotKey'];
57
58
                        $localObject = $localModel::where($findColumn, $record->{$findColumn})->first();
59
60
                        $datas = DBM::Object()->setManyToManyRelation($localObject, $foreignModel, $pivotTable, $parentPivotKey, $relatedPivotKey)->belongs_to_many;
0 ignored issues
show
Bug introduced by
The property belongs_to_many does not seem to exist on CodexShaper\DBM\Models\DBM_Object. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
61
62
                        $record->{$field->name}  = $datas;
63
                        $field->displayLabel     = $displayLabel;
64
                        $field->localKey         = $localKey;
65
                        $field->foreignKey       = $foreignKey;
66
                        $field->relationshipType = $relationshipType;
67
                    }
68
                }
69
            }
70
        }
71
72
        return $records;
73
    }
74
75
    public function storeRelationshipData($fields, $columns, $object, $table)
76
    {
77
        foreach ($fields as $field) {
78
79
            if (isset($field->relationship) && $field->relationship->relationType == "belongsToMany") {
80
81
                $relationship = $field->relationship;
82
83
                $localModel      = $relationship->localModel;
84
                $localTable      = $relationship->localTable;
85
                $foreignModel    = $relationship->foreignModel;
86
                $pivotTable      = $relationship->pivotTable;
87
                $parentPivotKey  = $relationship->parentPivotKey;
88
                $relatedPivotKey = $relationship->relatedPivotKey;
89
90
                $findColumn = $object->details['findColumn'];
91
92
                $localObject = DBM::model($localModel, $localTable)::where($findColumn, $table->{$findColumn})->first();
93
94
                DBM::Object()
95
                    ->setManyToManyRelation(
96
                        $localObject,
97
                        $foreignModel,
98
                        $pivotTable,
99
                        $parentPivotKey,
100
                        $relatedPivotKey
101
                    )
102
                    ->belongs_to_many()
103
                    ->attach($columns->{$relatedPivotKey});
104
            }
105
        }
106
    }
107
108
    public function updateRelationshipData($fields, $columns, $object, $table)
109
    {
110
        foreach ($fields as $field) {
111
112
            if (isset($field->relationship)) {
113
114
                $relationship = $field->relationship;
115
116
                $localModel   = $relationship->localModel;
117
                $localTable   = $relationship->localTable;
118
                $foreignModel = $relationship->foreignModel;
119
120
                if ($field->relationship->relationType == "belongsToMany") {
121
                    $pivotTable      = $relationship->pivotTable;
122
                    $parentPivotKey  = $relationship->parentPivotKey;
123
                    $relatedPivotKey = $relationship->relatedPivotKey;
124
125
                    $findColumn = $object->details['findColumn'];
126
127
                    $localObject = DBM::model($localModel, $localTable)->where($findColumn, $table->{$findColumn})->first();
128
129
                    DBM::Object()
130
                        ->setManyToManyRelation(
131
                            $localObject,
132
                            $foreignModel,
133
                            $pivotTable,
134
                            $parentPivotKey,
135
                            $relatedPivotKey
136
                        )
137
                        ->belongs_to_many()
138
                        ->sync($columns->{$relatedPivotKey});
139
                }
140
141
            }
142
        }
143
    }
144
145
    public function removeRelationshipData($field, $object, $table)
146
    {
147
        if ($field->type == 'relationship') {
148
149
            $relationship = $field->settings;
150
151
            $localModel   = $relationship->localModel;
152
            $foreignModel = $relationship->foreignModel;
153
154
            $findColumn = $object->details['findColumn'];
155
156
            $localObject = $localModel::where($findColumn, $table->{$findColumn})->first();
157
158
            if ($relationship->relationType == 'belongsToMany') {
159
160
                $pivotTable      = $relationship->pivotTable;
161
                $parentPivotKey  = $relationship->parentPivotKey;
162
                $relatedPivotKey = $relationship->relatedPivotKey;
163
164
                DBM::Object()
165
                    ->setManyToManyRelation(
166
                        $localObject,
167
                        $foreignModel,
168
                        $pivotTable,
169
                        $parentPivotKey,
170
                        $relatedPivotKey
171
                    )
172
                    ->belongs_to_many()
173
                    ->detach();
174
            } else if ($relationship->relationType == 'hasMany') {
175
176
                $foreignKey = $relationship->foreignKey;
177
                $localKey   = $relationship->localKey;
178
179
                DBM::Object()
180
                    ->setCommonRelation(
181
                        $localObject,
182
                        $foreignModel,
183
                        $foreignKey,
184
                        $localKey)
185
                    ->has_many()
186
                    ->delete();
187
            }
188
189
        }
190
    }
191
192
    public function removeRelationshipKeyForBelongsTo($fields, $foreignKey)
193
    {
194
        $results = [];
195
196
        foreach ($fields as $key => $field) {
197
            if ($field->name == $foreignKey) {
198
                unset($fields[$key]);
199
                continue;
200
            }
201
            $results[] = $field;
202
        }
203
204
        return $results;
205
    }
206
}
207