Skip to content

Commit bb1c7ef

Browse files
committed
Get the Other/Owner Key name based on different version of Illuminate/Database (mongodb#1116)
* get the Other/Owner Key name based on different version of Illuminate/Database * get the Other/Owner Key name based on different version of Illuminate/Database (all relations) * change function name to more readable
1 parent fa7b75d commit bb1c7ef

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/Jenssegers/Mongodb/Relations/BelongsTo.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function addConstraints()
1111
// For belongs to relationships, which are essentially the inverse of has one
1212
// or has many relationships, we need to actually query on the primary key
1313
// of the related models matching on the foreign key that's on a parent.
14-
$this->query->where($this->otherKey, '=', $this->parent->{$this->foreignKey});
14+
$this->query->where($this->getOwnerKey(), '=', $this->parent->{$this->foreignKey});
1515
}
1616
}
1717

@@ -23,8 +23,18 @@ public function addEagerConstraints(array $models)
2323
// We'll grab the primary key name of the related models since it could be set to
2424
// a non-standard name and not "id". We will then construct the constraint for
2525
// our eagerly loading query so it returns the proper models from execution.
26-
$key = $this->otherKey;
26+
$key = $this->getOwnerKey();
2727

2828
$this->query->whereIn($key, $this->getEagerModelKeys($models));
2929
}
30+
31+
/**
32+
* get the Other/Owner Key name based on different version of Illuminate/Database
33+
* see commit https://github.com/illuminate/database/commit/6a35698d72e276f435324b7e29b3cd37ef7d5d9c
34+
* @return string
35+
*/
36+
public function getOwnerKey()
37+
{
38+
return property_exists($this, "ownerKey") ? $this->ownerKey : $this->otherKey;
39+
}
3040
}

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function sync($ids, $detaching = true)
9696
// First we need to attach any of the associated models that are not currently
9797
// in this joining table. We'll spin through the given IDs, checking to see
9898
// if they exist in the array of current ones, and if not we will insert.
99-
$current = $this->parent->{$this->otherKey} ?: [];
99+
$current = $this->parent->{$this->getOwnerKey()} ?: [];
100100

101101
// See issue #256.
102102
if ($current instanceof Collection) {
@@ -170,7 +170,7 @@ public function attach($id, array $attributes = [], $touch = true)
170170
}
171171

172172
// Attach the new ids to the parent model.
173-
$this->parent->push($this->otherKey, (array) $id, true);
173+
$this->parent->push($this->getOwnerKey(), (array) $id, true);
174174

175175
if ($touch) {
176176
$this->touchIfTouching();
@@ -194,7 +194,7 @@ public function detach($ids = [], $touch = true)
194194
$ids = (array) $ids;
195195

196196
// Detach all ids from the parent model.
197-
$this->parent->pull($this->otherKey, $ids);
197+
$this->parent->pull($this->getOwnerKey(), $ids);
198198

199199
// Prepare the query to select all related objects.
200200
if (count($ids) > 0) {
@@ -279,4 +279,14 @@ protected function formatSyncList(array $records)
279279
}
280280
return $results;
281281
}
282+
283+
/**
284+
* get the Other/Owner Key name based on different version of Illuminate/Database
285+
* see commit https://github.com/illuminate/database/commit/6a35698d72e276f435324b7e29b3cd37ef7d5d9c
286+
* @return string
287+
*/
288+
public function getOwnerKey()
289+
{
290+
return property_exists($this, "ownerKey") ? $this->ownerKey : $this->otherKey;
291+
}
282292
}

src/Jenssegers/Mongodb/Relations/MorphTo.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function addConstraints()
1313
// For belongs to relationships, which are essentially the inverse of has one
1414
// or has many relationships, we need to actually query on the primary key
1515
// of the related models matching on the foreign key that's on a parent.
16-
$this->query->where($this->otherKey, '=', $this->parent->{$this->foreignKey});
16+
$this->query->where($this->getOwnerKey(), '=', $this->parent->{$this->foreignKey});
1717
}
1818
}
1919

@@ -30,4 +30,14 @@ protected function getResultsByType($type)
3030

3131
return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
3232
}
33+
34+
/**
35+
* get the Other/Owner Key name based on different version of Illuminate/Database
36+
* see commit https://github.com/illuminate/database/commit/6a35698d72e276f435324b7e29b3cd37ef7d5d9c
37+
* @return string
38+
*/
39+
public function getOwnerKey()
40+
{
41+
return property_exists($this, "ownerKey") ? $this->ownerKey : $this->otherKey;
42+
}
3343
}

0 commit comments

Comments
 (0)