Skip to content

Get the Other/Owner Key name based on different version of Illuminate/Database #1116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Jenssegers/Mongodb/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function addConstraints()
// 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});
}
}

Expand All @@ -25,8 +25,18 @@ public function addEagerConstraints(array $models)
// 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;
}
}
16 changes: 13 additions & 3 deletions src/Jenssegers/Mongodb/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function sync($ids, $detaching = true)
// 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) {
Expand Down Expand Up @@ -192,7 +192,7 @@ public function attach($id, array $attributes = [], $touch = true)
}

// 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();
Expand Down Expand Up @@ -220,7 +220,7 @@ public function detach($ids = [], $touch = true)
$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) {
Expand Down Expand Up @@ -310,4 +310,14 @@ protected function formatSyncList(array $records)
}
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;
}
}
12 changes: 11 additions & 1 deletion src/Jenssegers/Mongodb/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function addConstraints()
// 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});
}
}

Expand All @@ -33,4 +33,14 @@ protected function getResultsByType($type)

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;
}
}