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] Fix morphTo without SoftDeletes #13806

Merged
merged 2 commits into from
Jun 1, 2016
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
22 changes: 18 additions & 4 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,11 @@ protected function getResultsByType($type)

$key = $instance->getTable().'.'.$instance->getKeyName();

$query = clone $this->query;
$query = $instance->newQuery();

$query->setEagerLoads($this->getEagerLoadsForInstance($instance));

$query->setModel($instance);

$query->useConnection($instance->getConnection());
$this->mergeRelationWheresToMorphQuery($this->query, $query);

return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
}
Expand All @@ -205,6 +203,22 @@ protected function getEagerLoadsForInstance(Model $instance)
})->merge($instance->getEagerLoads())->all();
}

/**
* Merge the "wheres" from a relation query to a morph query.
*
* @param \Illuminate\Database\Eloquent\Builder $relationQuery
* @param \Illuminate\Database\Eloquent\Builder $morphQuery
* @return void
*/
protected function mergeRelationWheresToMorphQuery(Builder $relationQuery, Builder $morphQuery)
{
$removedScopes = $relationQuery->removedScopes();

$morphQuery->withoutGlobalScopes($removedScopes)->mergeWheres(
$relationQuery->getQuery()->wheres, $relationQuery->getBindings()
);
}

/**
* Gather all of the foreign keys for a given type.
*
Expand Down
17 changes: 0 additions & 17 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2321,23 +2321,6 @@ public function useWritePdo()
return $this;
}

/**
* Use a different connection for the query.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Illuminate\Database\Query\Grammars\Grammar $grammar
* @param \Illuminate\Database\Query\Processors\Processor $processor
* @return void
*/
public function useConnection(ConnectionInterface $connection,
Grammar $grammar = null,
Processor $processor = null)
{
$this->connection = $connection;
$this->grammar = $grammar ?: $connection->getQueryGrammar();
$this->processor = $processor ?: $connection->getPostProcessor();
}

/**
* Handle dynamic method calls into the method.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,26 @@ public function testMorphToWithoutConstraints()
$this->assertEquals(null, $comment->owner);
}

public function testMorphToNonSoftDeletingModel()
{
$taylor = TestUserWithoutSoftDelete::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
$post1 = $taylor->posts()->create(['title' => 'First Title']);
$post1->comments()->create([
'body' => 'Comment Body',
'owner_type' => TestUserWithoutSoftDelete::class,
'owner_id' => $taylor->id,
]);

$comment = SoftDeletesTestCommentWithTrashed::with('owner')->first();

$this->assertEquals($taylor->email, $comment->owner->email);

$taylor->delete();
$comment = SoftDeletesTestCommentWithTrashed::with('owner')->first();

$this->assertEquals(null, $comment->owner);
}

/**
* Helpers...
*/
Expand Down Expand Up @@ -611,6 +631,20 @@ protected function schema()
}
}

/**
* Eloquent Models...
*/
class TestUserWithoutSoftDelete extends Eloquent
{
protected $table = 'users';
protected $guarded = [];

public function posts()
{
return $this->hasMany(SoftDeletesTestPost::class, 'user_id');
}
}

/**
* Eloquent Models...
*/
Expand Down