Skip to content

Commit 3e26e05

Browse files
committed
Fix issue with renamed properties for belongsTo and belongsToMany
1 parent d9543de commit 3e26e05

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

src/Jenssegers/Mongodb/Eloquent/HybridRelations.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ trait HybridRelations
2222
public function hasOne($related, $foreignKey = null, $localKey = null)
2323
{
2424
// Check if it is a relation with an original model.
25-
if (! is_subclass_of($related, 'Jenssegers\Mongodb\Eloquent\Model')) {
25+
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
2626
return parent::hasOne($related, $foreignKey, $localKey);
2727
}
2828

@@ -48,16 +48,14 @@ public function hasOne($related, $foreignKey = null, $localKey = null)
4848
public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
4949
{
5050
// Check if it is a relation with an original model.
51-
if (! is_subclass_of($related, 'Jenssegers\Mongodb\Eloquent\Model')) {
51+
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
5252
return parent::morphOne($related, $name, $type, $id, $localKey);
5353
}
5454

5555
$instance = new $related;
5656

5757
list($type, $id) = $this->getMorphs($name, $type, $id);
5858

59-
$table = $instance->getTable();
60-
6159
$localKey = $localKey ?: $this->getKeyName();
6260

6361
return new MorphOne($instance->newQuery(), $this, $type, $id, $localKey);
@@ -74,7 +72,7 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey =
7472
public function hasMany($related, $foreignKey = null, $localKey = null)
7573
{
7674
// Check if it is a relation with an original model.
77-
if (! is_subclass_of($related, 'Jenssegers\Mongodb\Eloquent\Model')) {
75+
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
7876
return parent::hasMany($related, $foreignKey, $localKey);
7977
}
8078

@@ -100,7 +98,7 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
10098
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
10199
{
102100
// Check if it is a relation with an original model.
103-
if (! is_subclass_of($related, 'Jenssegers\Mongodb\Eloquent\Model')) {
101+
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
104102
return parent::morphMany($related, $name, $type, $id, $localKey);
105103
}
106104

@@ -139,7 +137,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
139137
}
140138

141139
// Check if it is a relation with an original model.
142-
if (! is_subclass_of($related, 'Jenssegers\Mongodb\Eloquent\Model')) {
140+
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
143141
return parent::belongsTo($related, $foreignKey, $otherKey, $relation);
144142
}
145143

@@ -222,11 +220,11 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
222220
// name of the calling function. We will use that function name as the
223221
// title of this relation since that is a great convention to apply.
224222
if (is_null($relation)) {
225-
$relation = $this->getBelongsToManyCaller();
223+
$relation = $this->guessBelongsToManyRelation();
226224
}
227225

228226
// Check if it is a relation with an original model.
229-
if (! is_subclass_of($related, 'Jenssegers\Mongodb\Eloquent\Model')) {
227+
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
230228
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
231229
}
232230

@@ -253,4 +251,18 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
253251

254252
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
255253
}
254+
255+
/**
256+
* Get the relationship name of the belongs to many.
257+
*
258+
* @return string
259+
*/
260+
protected function guessBelongsToManyRelation()
261+
{
262+
if (method_exists($this, 'getBelongsToManyCaller')) {
263+
return $this->getBelongsToManyCaller();
264+
}
265+
266+
return parent::guessBelongsToManyRelation();
267+
}
256268
}

src/Jenssegers/Mongodb/Relations/BelongsTo.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ public function addEagerConstraints(array $models)
2929
}
3030

3131
/**
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
32+
* Get the owner key with backwards compatible support.
3533
*/
3634
public function getOwnerKey()
3735
{
38-
return property_exists($this, "ownerKey") ? $this->ownerKey : $this->otherKey;
36+
return property_exists($this, 'ownerKey') ? $this->ownerKey : $this->otherKey;
3937
}
4038
}

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 7 additions & 7 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->getOwnerKey()} ?: [];
99+
$current = $this->parent->{$this->getRelatedKey()} ?: [];
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->getOwnerKey(), (array) $id, true);
173+
$this->parent->push($this->getRelatedKey(), (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->getOwnerKey(), $ids);
197+
$this->parent->pull($this->getRelatedKey(), $ids);
198198

199199
// Prepare the query to select all related objects.
200200
if (count($ids) > 0) {
@@ -281,12 +281,12 @@ protected function formatSyncList(array $records)
281281
}
282282

283283
/**
284-
* get the Other/Owner Key name based on different version of Illuminate/Database
285-
* see commit https://github.com/illuminate/database/commit/6a35698d72e276f435324b7e29b3cd37ef7d5d9c
284+
* Get the related key with backwards compatible support.
285+
*
286286
* @return string
287287
*/
288-
public function getOwnerKey()
288+
public function getRelatedKey()
289289
{
290-
return property_exists($this, "ownerKey") ? $this->ownerKey : $this->otherKey;
290+
return property_exists($this, 'relatedKey') ? $this->relatedKey : $this->otherKey;
291291
}
292292
}

0 commit comments

Comments
 (0)