Skip to content

Commit

Permalink
[5.2] Fix morphTo without SoftDeletes (#13806)
Browse files Browse the repository at this point in the history
* Fix morphTo

* Add test
  • Loading branch information
acasar authored and taylorotwell committed Jun 1, 2016
1 parent 4d7eb59 commit 8244d24
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
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

0 comments on commit 8244d24

Please sign in to comment.