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.8] Fix wrong class being used when eager loading nullable MorphTo with withDefault() #27455

Merged
merged 1 commit into from
Feb 12, 2019
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
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ public function touch()
}
}

/**
* Make a new related instance for the given model.
*
* @param \Illuminate\Database\Eloquent\Model $parent
* @return \Illuminate\Database\Eloquent\Model
*/
protected function newRelatedInstanceFor(Model $parent)
{
return $parent->{$this->getRelationName()}()->getRelated()->newInstance();
}

/**
* Get the foreign key "type" name.
*
Expand Down
44 changes: 35 additions & 9 deletions tests/Database/DatabaseEloquentMorphToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ public function testMorphToWithDefault()

$newModel = new EloquentMorphToModelStub;

$this->related->shouldReceive('newInstance')->once()->andReturn($newModel);

$this->assertSame($newModel, $relation->getResults());
$this->assertEquals($newModel, $relation->getResults());
}

public function testMorphToWithDynamicDefault()
Expand All @@ -67,12 +65,13 @@ public function testMorphToWithDynamicDefault()
$this->builder->shouldReceive('first')->once()->andReturnNull();

$newModel = new EloquentMorphToModelStub;
$newModel->username = 'taylor';

$this->related->shouldReceive('newInstance')->once()->andReturn($newModel);
$result = $relation->getResults();

$this->assertSame($newModel, $relation->getResults());
$this->assertEquals($newModel, $result);

$this->assertSame('taylor', $newModel->username);
$this->assertSame('taylor', $result->username);
}

public function testMorphToWithArrayDefault()
Expand All @@ -82,12 +81,27 @@ public function testMorphToWithArrayDefault()
$this->builder->shouldReceive('first')->once()->andReturnNull();

$newModel = new EloquentMorphToModelStub;
$newModel->username = 'taylor';

$result = $relation->getResults();

$this->assertEquals($newModel, $result);

$this->assertSame('taylor', $result->username);
}

public function testMorphToWithSpecifiedClassDefault()
{
$parent = new EloquentMorphToModelStub;
$parent->relation_type = EloquentMorphToRelatedStub::class;

$relation = $parent->relation()->withDefault();

$this->related->shouldReceive('newInstance')->once()->andReturn($newModel);
$newModel = new EloquentMorphToRelatedStub;

$this->assertSame($newModel, $relation->getResults());
$result = $relation->getResults();

$this->assertSame('taylor', $newModel->username);
$this->assertEquals($newModel, $result);
}

public function testAssociateMethodSetsForeignKeyAndTypeOnModel()
Expand Down Expand Up @@ -165,4 +179,16 @@ public function getRelation($parent = null, $builder = null)
class EloquentMorphToModelStub extends Model
{
public $foreign_key = 'foreign.value';

public $table = 'eloquent_morph_to_model_stubs';

public function relation()
{
return $this->morphTo();
}
}

class EloquentMorphToRelatedStub extends Model
{
public $table = 'eloquent_morph_to_related_stubs';
}