Skip to content

Commit

Permalink
Allow setting custom morphTo ownerKey. (#21310)
Browse files Browse the repository at this point in the history
  • Loading branch information
Víctor Díaz authored and taylorotwell committed Sep 21, 2017
1 parent 5bfedaa commit c04958b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
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

0 comments on commit c04958b

Please sign in to comment.