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.2] Apply constraints to a morphTo relation on eager load #13724

Merged
merged 8 commits into from
May 26, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 5 additions & 35 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ class MorphTo extends BelongsTo
*/
protected $dictionary = [];

/*
* Indicates if soft-deleted model instances should be fetched.
*
* @var bool
*/
protected $withTrashed = false;

/**
* Create a new morph to relationship instance.
*
Expand Down Expand Up @@ -186,6 +179,11 @@ protected function getResultsByType($type)

$query = $this->useWithTrashed($query);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should remove this line too


$query = $query->withoutGlobalScopes($this->query->removedScopes());
foreach ($this->query->getQuery()->getRawBindings() as $bindingType => $bindings) {
$query->setBindings($bindings, $bindingType);
}

return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
}

Expand Down Expand Up @@ -238,32 +236,4 @@ public function getDictionary()
return $this->dictionary;
}

/**
* Fetch soft-deleted model instances with query.
*
* @return $this
*/
public function withTrashed()
{
$this->withTrashed = true;

$this->query = $this->useWithTrashed($this->query);

return $this;
}

/**
* Return trashed models with query if told so.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function useWithTrashed(Builder $query)
{
if ($this->withTrashed && $query->getMacro('withTrashed') !== null) {
return $query->withTrashed();
}

return $query;
}
}
16 changes: 16 additions & 0 deletions tests/Database/DatabaseEloquentMorphToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public function testModelsAreProperlyPulledAndMatched()
$secondQuery->shouldReceive('getTable')->andReturn('foreign_table_2');
$secondQuery->shouldReceive('getKeyName')->andReturn('id');

$firstQuery->shouldReceive('withoutGlobalScopes')->with([])->andReturn($firstQuery);
$firstQuery->shouldReceive('setBindings')->andReturnNull();
$secondQuery->shouldReceive('withoutGlobalScopes')->with([])->andReturn($secondQuery);
$secondQuery->shouldReceive('setBindings')->andReturnNull();

$firstQuery->shouldReceive('newQuery')->once()->andReturn($firstQuery);
$secondQuery->shouldReceive('newQuery')->once()->andReturn($secondQuery);

Expand Down Expand Up @@ -139,6 +144,17 @@ protected function getRelationAssociate($parent)
public function getRelation($parent = null, $builder = null)
{
$builder = $builder ?: m::mock('Illuminate\Database\Eloquent\Builder');
$builder->shouldReceive('getQuery')->andReturn($builder);
$builder->shouldReceive('removedScopes')->andReturn([]);
$builder->shouldReceive('withoutGlobalScopes')->with([])->andReturn($builder);
$builder->shouldReceive('getRawBindings')->andReturn([
'select' => [],
'join' => [],
'where' => [],
'having' => [],
'order' => [],
'union' => [],
]);
$builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value');
$related = m::mock('Illuminate\Database\Eloquent\Model');
$related->shouldReceive('getKeyName')->andReturn('id');
Expand Down