Commit 1200af46 authored by Eslam Salem's avatar Eslam Salem Committed by Jens Segers

Get the Other/Owner Key name based on different version of Illuminate/Database (#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
parent c76f333e
......@@ -11,7 +11,7 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo
// For belongs to relationships, which are essentially the inverse of has one
// or has many relationships, we need to actually query on the primary key
// of the related models matching on the foreign key that's on a parent.
$this->query->where($this->otherKey, '=', $this->parent->{$this->foreignKey});
$this->query->where($this->getOwnerKey(), '=', $this->parent->{$this->foreignKey});
}
}
......@@ -23,8 +23,18 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo
// We'll grab the primary key name of the related models since it could be set to
// a non-standard name and not "id". We will then construct the constraint for
// our eagerly loading query so it returns the proper models from execution.
$key = $this->otherKey;
$key = $this->getOwnerKey();
$this->query->whereIn($key, $this->getEagerModelKeys($models));
}
/**
* get the Other/Owner Key name based on different version of Illuminate/Database
* see commit https://github.com/illuminate/database/commit/6a35698d72e276f435324b7e29b3cd37ef7d5d9c
* @return string
*/
public function getOwnerKey()
{
return property_exists($this, "ownerKey") ? $this->ownerKey : $this->otherKey;
}
}
......@@ -96,7 +96,7 @@ class BelongsToMany extends EloquentBelongsToMany
// 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
// if they exist in the array of current ones, and if not we will insert.
$current = $this->parent->{$this->otherKey} ?: [];
$current = $this->parent->{$this->getOwnerKey()} ?: [];
// See issue #256.
if ($current instanceof Collection) {
......@@ -170,7 +170,7 @@ class BelongsToMany extends EloquentBelongsToMany
}
// Attach the new ids to the parent model.
$this->parent->push($this->otherKey, (array) $id, true);
$this->parent->push($this->getOwnerKey(), (array) $id, true);
if ($touch) {
$this->touchIfTouching();
......@@ -194,7 +194,7 @@ class BelongsToMany extends EloquentBelongsToMany
$ids = (array) $ids;
// Detach all ids from the parent model.
$this->parent->pull($this->otherKey, $ids);
$this->parent->pull($this->getOwnerKey(), $ids);
// Prepare the query to select all related objects.
if (count($ids) > 0) {
......@@ -279,4 +279,14 @@ class BelongsToMany extends EloquentBelongsToMany
}
return $results;
}
/**
* get the Other/Owner Key name based on different version of Illuminate/Database
* see commit https://github.com/illuminate/database/commit/6a35698d72e276f435324b7e29b3cd37ef7d5d9c
* @return string
*/
public function getOwnerKey()
{
return property_exists($this, "ownerKey") ? $this->ownerKey : $this->otherKey;
}
}
......@@ -13,7 +13,7 @@ class MorphTo extends EloquentMorphTo
// For belongs to relationships, which are essentially the inverse of has one
// or has many relationships, we need to actually query on the primary key
// of the related models matching on the foreign key that's on a parent.
$this->query->where($this->otherKey, '=', $this->parent->{$this->foreignKey});
$this->query->where($this->getOwnerKey(), '=', $this->parent->{$this->foreignKey});
}
}
......@@ -30,4 +30,14 @@ class MorphTo extends EloquentMorphTo
return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
}
/**
* get the Other/Owner Key name based on different version of Illuminate/Database
* see commit https://github.com/illuminate/database/commit/6a35698d72e276f435324b7e29b3cd37ef7d5d9c
* @return string
*/
public function getOwnerKey()
{
return property_exists($this, "ownerKey") ? $this->ownerKey : $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