Completed
Push — master ( fbf6ba...24a065 )
by CodexShaper
04:39
created

RecordRelationship::removeRelationshipKeyForBelongsTo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 12
rs 10
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
    protected $has_many;
11
    protected $belongs_to_many;
12
    protected $belongs_to;
13
14
    public function prepareRelationshipData($records, $browseFields, $object)
15
    {
16
        foreach ($records as $item => $record) {
17
18
            foreach ($browseFields as $field) {
19
20
                if ($field->type == 'relationship') {
21
22
                    $relationship = $field->settings;
23
24
                    $findColumn = $object->details['findColumn'];
25
26
                    $localModel       = $relationship['localModel'];
27
                    $localKey         = $relationship['localKey'];
28
                    $foreignModel     = $relationship['foreignModel'];
29
                    $foreignKey       = $relationship['foreignKey'];
30
                    $relationshipType = $relationship['relationType'];
31
                    $displayLabel     = $relationship['displayLabel'];
32
33
                    if ($relationshipType == 'belongsTo') {
34
35
                        $localObject = $localModel::where($findColumn, $record->{$findColumn})->first();
36
37
                        $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...
38
39
                        $record->{$field->name}  = $datas;
40
                        $field->displayLabel     = $displayLabel;
41
                        $field->localKey         = $localKey;
42
                        $field->foreignKey       = $foreignKey;
43
                        $field->relationshipType = $relationshipType;
44
45
                    } else if ($relationshipType == 'hasMany') {
46
47
                        $localObject = $localModel::where($findColumn, $record->{$findColumn})->first();
48
                        $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...
49
50
                        $record->{$field->name}  = $datas;
51
                        $field->displayLabel     = $displayLabel;
52
                        $field->localKey         = $localKey;
53
                        $field->foreignKey       = $foreignKey;
54
                        $field->relationshipType = $relationshipType;
55
56
                    } else if ($relationshipType == 'belongsToMany') {
57
58
                        $pivotTable      = $relationship['pivotTable'];
59
                        $parentPivotKey  = $relationship['parentPivotKey'];
60
                        $relatedPivotKey = $relationship['relatedPivotKey'];
61
62
                        $localObject = $localModel::where($findColumn, $record->{$findColumn})->first();
63
64
                        $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...
65
66
                        $record->{$field->name}  = $datas;
67
                        $field->displayLabel     = $displayLabel;
68
                        $field->localKey         = $localKey;
69
                        $field->foreignKey       = $foreignKey;
70
                        $field->relationshipType = $relationshipType;
71
                    }
72
                }
73
            }
74
        }
75
76
        return $records;
77
    }
78
79
    public function storeRelationshipData($fields, $columns, $object, $table)
80
    {
81
        foreach ($fields as $field) {
82
83
            if (isset($field->relationship) && $field->relationship->relationType == "belongsToMany") {
84
85
                $relationship = $field->relationship;
86
87
                $localModel      = $relationship->localModel;
88
                $localTable      = $relationship->localTable;
89
                $foreignModel    = $relationship->foreignModel;
90
                $pivotTable      = $relationship->pivotTable;
91
                $parentPivotKey  = $relationship->parentPivotKey;
92
                $relatedPivotKey = $relationship->relatedPivotKey;
93
94
                $findColumn = $object->details['findColumn'];
95
96
                $localObject = DBM::model($localModel, $localTable)::where($findColumn, $table->{$findColumn})->first();
97
98
                DBM::Object()
99
                    ->setManyToManyRelation(
100
                        $localObject,
101
                        $foreignModel,
102
                        $pivotTable,
103
                        $parentPivotKey,
104
                        $relatedPivotKey
105
                    )
106
                    ->belongs_to_many()
107
                    ->attach($columns->{$relatedPivotKey});
108
            }
109
        }
110
    }
111
112
    public function updateRelationshipData($fields, $columns, $object, $table)
113
    {
114
        foreach ($fields as $field) {
115
116
            if (isset($field->relationship)) {
117
118
                $relationship = $field->relationship;
119
120
                $localModel   = $relationship->localModel;
121
                $localTable   = $relationship->localTable;
122
                $foreignModel = $relationship->foreignModel;
123
124
                if ($field->relationship->relationType == "belongsToMany") {
125
                    $pivotTable      = $relationship->pivotTable;
126
                    $parentPivotKey  = $relationship->parentPivotKey;
127
                    $relatedPivotKey = $relationship->relatedPivotKey;
128
                    $findColumn      = $object->details['findColumn'];
129
130
                    $localObject = DBM::model($localModel, $localTable)->where($findColumn, $table->{$findColumn})->first();
131
132
                    DBM::Object()
133
                        ->setManyToManyRelation(
134
                            $localObject,
135
                            $foreignModel,
136
                            $pivotTable,
137
                            $parentPivotKey,
138
                            $relatedPivotKey
139
                        )
140
                        ->belongs_to_many()
141
                        ->sync($columns->{$relatedPivotKey});
142
                }
143
144
            }
145
        }
146
    }
147
148
    public function removeRelationshipData($field, $object, $table)
149
    {
150
        if ($field->type == 'relationship') {
151
152
            $relationship = $field->settings;
153
154
            $localModel   = $relationship->localModel;
155
            $foreignModel = $relationship->foreignModel;
156
157
            $findColumn = $object->details['findColumn'];
158
159
            $localObject = $localModel::where($findColumn, $table->{$findColumn})->first();
160
161
            if ($relationship->relationType == 'belongsToMany') {
162
163
                $pivotTable      = $relationship->pivotTable;
164
                $parentPivotKey  = $relationship->parentPivotKey;
165
                $relatedPivotKey = $relationship->relatedPivotKey;
166
167
                DBM::Object()
168
                    ->setManyToManyRelation(
169
                        $localObject,
170
                        $foreignModel,
171
                        $pivotTable,
172
                        $parentPivotKey,
173
                        $relatedPivotKey
174
                    )
175
                    ->belongs_to_many()
176
                    ->detach();
177
            } else if ($relationship->relationType == 'hasMany') {
178
179
                $foreignKey = $relationship->foreignKey;
180
                $localKey   = $relationship->localKey;
181
182
                DBM::Object()
183
                    ->setCommonRelation(
184
                        $localObject,
185
                        $foreignModel,
186
                        $foreignKey,
187
                        $localKey)
188
                    ->has_many()
189
                    ->delete();
190
            }
191
192
        }
193
    }
194
}
195