Commit 3e26e05b authored by Jens Segers's avatar Jens Segers

Fix issue with renamed properties for belongsTo and belongsToMany

parent d9543de4
...@@ -22,7 +22,7 @@ trait HybridRelations ...@@ -22,7 +22,7 @@ trait HybridRelations
public function hasOne($related, $foreignKey = null, $localKey = null) public function hasOne($related, $foreignKey = null, $localKey = null)
{ {
// Check if it is a relation with an original model. // Check if it is a relation with an original model.
if (! is_subclass_of($related, 'Jenssegers\Mongodb\Eloquent\Model')) { if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return parent::hasOne($related, $foreignKey, $localKey); return parent::hasOne($related, $foreignKey, $localKey);
} }
...@@ -48,7 +48,7 @@ trait HybridRelations ...@@ -48,7 +48,7 @@ trait HybridRelations
public function morphOne($related, $name, $type = null, $id = null, $localKey = null) public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
{ {
// Check if it is a relation with an original model. // Check if it is a relation with an original model.
if (! is_subclass_of($related, 'Jenssegers\Mongodb\Eloquent\Model')) { if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return parent::morphOne($related, $name, $type, $id, $localKey); return parent::morphOne($related, $name, $type, $id, $localKey);
} }
...@@ -56,8 +56,6 @@ trait HybridRelations ...@@ -56,8 +56,6 @@ trait HybridRelations
list($type, $id) = $this->getMorphs($name, $type, $id); list($type, $id) = $this->getMorphs($name, $type, $id);
$table = $instance->getTable();
$localKey = $localKey ?: $this->getKeyName(); $localKey = $localKey ?: $this->getKeyName();
return new MorphOne($instance->newQuery(), $this, $type, $id, $localKey); return new MorphOne($instance->newQuery(), $this, $type, $id, $localKey);
...@@ -74,7 +72,7 @@ trait HybridRelations ...@@ -74,7 +72,7 @@ trait HybridRelations
public function hasMany($related, $foreignKey = null, $localKey = null) public function hasMany($related, $foreignKey = null, $localKey = null)
{ {
// Check if it is a relation with an original model. // Check if it is a relation with an original model.
if (! is_subclass_of($related, 'Jenssegers\Mongodb\Eloquent\Model')) { if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return parent::hasMany($related, $foreignKey, $localKey); return parent::hasMany($related, $foreignKey, $localKey);
} }
...@@ -100,7 +98,7 @@ trait HybridRelations ...@@ -100,7 +98,7 @@ trait HybridRelations
public function morphMany($related, $name, $type = null, $id = null, $localKey = null) public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
{ {
// Check if it is a relation with an original model. // Check if it is a relation with an original model.
if (! is_subclass_of($related, 'Jenssegers\Mongodb\Eloquent\Model')) { if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return parent::morphMany($related, $name, $type, $id, $localKey); return parent::morphMany($related, $name, $type, $id, $localKey);
} }
...@@ -139,7 +137,7 @@ trait HybridRelations ...@@ -139,7 +137,7 @@ trait HybridRelations
} }
// Check if it is a relation with an original model. // Check if it is a relation with an original model.
if (! is_subclass_of($related, 'Jenssegers\Mongodb\Eloquent\Model')) { if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return parent::belongsTo($related, $foreignKey, $otherKey, $relation); return parent::belongsTo($related, $foreignKey, $otherKey, $relation);
} }
...@@ -222,11 +220,11 @@ trait HybridRelations ...@@ -222,11 +220,11 @@ trait HybridRelations
// name of the calling function. We will use that function name as the // name of the calling function. We will use that function name as the
// title of this relation since that is a great convention to apply. // title of this relation since that is a great convention to apply.
if (is_null($relation)) { if (is_null($relation)) {
$relation = $this->getBelongsToManyCaller(); $relation = $this->guessBelongsToManyRelation();
} }
// Check if it is a relation with an original model. // Check if it is a relation with an original model.
if (! is_subclass_of($related, 'Jenssegers\Mongodb\Eloquent\Model')) { if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation); return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
} }
...@@ -253,4 +251,18 @@ trait HybridRelations ...@@ -253,4 +251,18 @@ trait HybridRelations
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation); return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
} }
/**
* Get the relationship name of the belongs to many.
*
* @return string
*/
protected function guessBelongsToManyRelation()
{
if (method_exists($this, 'getBelongsToManyCaller')) {
return $this->getBelongsToManyCaller();
}
return parent::guessBelongsToManyRelation();
}
} }
...@@ -29,12 +29,10 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo ...@@ -29,12 +29,10 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo
} }
/** /**
* get the Other/Owner Key name based on different version of Illuminate/Database * Get the owner key with backwards compatible support.
* see commit https://github.com/illuminate/database/commit/6a35698d72e276f435324b7e29b3cd37ef7d5d9c
* @return string
*/ */
public function getOwnerKey() public function getOwnerKey()
{ {
return property_exists($this, "ownerKey") ? $this->ownerKey : $this->otherKey; return property_exists($this, 'ownerKey') ? $this->ownerKey : $this->otherKey;
} }
} }
...@@ -96,7 +96,7 @@ class BelongsToMany extends EloquentBelongsToMany ...@@ -96,7 +96,7 @@ class BelongsToMany extends EloquentBelongsToMany
// First we need to attach any of the associated models that are not currently // First we need to attach any of the associated models that are not currently
// in this joining table. We'll spin through the given IDs, checking to see // in this joining table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we will insert. // if they exist in the array of current ones, and if not we will insert.
$current = $this->parent->{$this->getOwnerKey()} ?: []; $current = $this->parent->{$this->getRelatedKey()} ?: [];
// See issue #256. // See issue #256.
if ($current instanceof Collection) { if ($current instanceof Collection) {
...@@ -170,7 +170,7 @@ class BelongsToMany extends EloquentBelongsToMany ...@@ -170,7 +170,7 @@ class BelongsToMany extends EloquentBelongsToMany
} }
// Attach the new ids to the parent model. // Attach the new ids to the parent model.
$this->parent->push($this->getOwnerKey(), (array) $id, true); $this->parent->push($this->getRelatedKey(), (array) $id, true);
if ($touch) { if ($touch) {
$this->touchIfTouching(); $this->touchIfTouching();
...@@ -194,7 +194,7 @@ class BelongsToMany extends EloquentBelongsToMany ...@@ -194,7 +194,7 @@ class BelongsToMany extends EloquentBelongsToMany
$ids = (array) $ids; $ids = (array) $ids;
// Detach all ids from the parent model. // Detach all ids from the parent model.
$this->parent->pull($this->getOwnerKey(), $ids); $this->parent->pull($this->getRelatedKey(), $ids);
// Prepare the query to select all related objects. // Prepare the query to select all related objects.
if (count($ids) > 0) { if (count($ids) > 0) {
...@@ -281,12 +281,12 @@ class BelongsToMany extends EloquentBelongsToMany ...@@ -281,12 +281,12 @@ class BelongsToMany extends EloquentBelongsToMany
} }
/** /**
* get the Other/Owner Key name based on different version of Illuminate/Database * Get the related key with backwards compatible support.
* see commit https://github.com/illuminate/database/commit/6a35698d72e276f435324b7e29b3cd37ef7d5d9c *
* @return string * @return string
*/ */
public function getOwnerKey() public function getRelatedKey()
{ {
return property_exists($this, "ownerKey") ? $this->ownerKey : $this->otherKey; return property_exists($this, 'relatedKey') ? $this->relatedKey : $this->otherKey;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment