Skip to content
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

[5.6] Allow setting custom morphTo ownerKey #21310

Merged
merged 1 commit into from
Sep 21, 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
17 changes: 10 additions & 7 deletions src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat
* @param string $name
* @param string $type
* @param string $id
* @param string $ownerKey
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function morphTo($name = null, $type = null, $id = null)
public function morphTo($name = null, $type = null, $id = null, $ownerKey = null)
{
// If no name is provided, we will use the backtrace to get the function name
// since that is most likely the name of the polymorphic interface. We can
Expand All @@ -145,8 +146,8 @@ public function morphTo($name = null, $type = null, $id = null)
// the relationship. In this case we'll just pass in a dummy query where we
// need to remove any eager loads that may already be defined on a model.
return empty($class = $this->{$type})
? $this->morphEagerTo($name, $type, $id)
: $this->morphInstanceTo($class, $name, $type, $id);
? $this->morphEagerTo($name, $type, $id, $ownerKey)
: $this->morphInstanceTo($class, $name, $type, $id, $ownerKey);
}

/**
Expand All @@ -155,12 +156,13 @@ public function morphTo($name = null, $type = null, $id = null)
* @param string $name
* @param string $type
* @param string $id
* @param string $ownerKey
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
protected function morphEagerTo($name, $type, $id)
protected function morphEagerTo($name, $type, $id, $ownerKey)
{
return new MorphTo(
$this->newQuery()->setEagerLoads([]), $this, $id, null, $type, $name
$this->newQuery()->setEagerLoads([]), $this, $id, $ownerKey, $type, $name
);
}

Expand All @@ -171,16 +173,17 @@ protected function morphEagerTo($name, $type, $id)
* @param string $name
* @param string $type
* @param string $id
* @param string $ownerKey
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
protected function morphInstanceTo($target, $name, $type, $id)
protected function morphInstanceTo($target, $name, $type, $id, $ownerKey)
{
$instance = $this->newRelatedInstance(
static::getActualClassNameForMorph($target)
);

return new MorphTo(
$instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
$instance->newQuery(), $this, $id, $ownerKey ?? $instance->getKeyName(), $type, $name
);
}

Expand Down
10 changes: 7 additions & 3 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ protected function getResultsByType($type)
{
$instance = $this->createModelByType($type);

$ownerKey = $this->ownerKey ?? $instance->getKeyName();

$query = $this->replayMacros($instance->newQuery())
->mergeConstraintsFrom($this->getQuery())
->with($this->getQuery()->getEagerLoads());

return $query->whereIn(
$instance->getTable().'.'.$instance->getKeyName(), $this->gatherKeysByType($type)
$instance->getTable().'.'.$ownerKey, $this->gatherKeysByType($type)
)->get();
}

Expand Down Expand Up @@ -178,8 +180,10 @@ public function match(array $models, Collection $results, $relation)
protected function matchToMorphParents($type, Collection $results)
{
foreach ($results as $result) {
if (isset($this->dictionary[$type][$result->getKey()])) {
foreach ($this->dictionary[$type][$result->getKey()] as $model) {
$ownerKey = ! is_null($this->ownerKey) ? $result->{$this->ownerKey} : $result->getKey();

if (isset($this->dictionary[$type][$ownerKey])) {
foreach ($this->dictionary[$type][$ownerKey] as $model) {
$model->setRelation($this->relation, $result);
}
}
Expand Down