Skip to content

Commit

Permalink
[6.x] Backport for fix issue with polymorphic morphMaps with literal 0 (
Browse files Browse the repository at this point in the history
#35364) (#35487)

* [8.x] Fix issue with polymorphic morphMaps with literal 0 (#35364)

* fix: use strict null check on morphTo type

* test: assert 0 relation_type is handled correctly

* Use setMethods for 6.x

* Update HasRelationships.php

Co-authored-by: Connor Tumbleson <iBotPeaches@users.noreply.github.com>
  • Loading branch information
driesvints and iBotPeaches committed Dec 4, 2020
1 parent 4822b4f commit 57797fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Expand Up @@ -233,7 +233,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
// If the type value is null it is probably safe to assume we're eager loading
// 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})
return is_null($class = $this->{$type}) || $class === ''
? $this->morphEagerTo($name, $type, $id, $ownerKey)
: $this->morphInstanceTo($class, $name, $type, $id, $ownerKey);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseEloquentMorphToTest.php
Expand Up @@ -90,6 +90,16 @@ public function testMorphToWithArrayDefault()
$this->assertSame('taylor', $result->username);
}

public function testMorphToWithZeroMorphType()
{
$parent = $this->getMockBuilder(EloquentMorphToModelStub::class)->setMethods(['getAttribute', 'morphEagerTo', 'morphInstanceTo'])->getMock();
$parent->method('getAttribute')->with('relation_type')->willReturn(0);
$parent->expects($this->once())->method('morphInstanceTo');
$parent->expects($this->never())->method('morphEagerTo');

$parent->relation();
}

public function testMorphToWithSpecifiedClassDefault()
{
$parent = new EloquentMorphToModelStub;
Expand Down

0 comments on commit 57797fa

Please sign in to comment.