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.4] Add make method to HasOneOrMany and MorphOneOrMany relations #19307

Merged
merged 2 commits into from
May 24, 2017
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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function __construct(QueryBuilder $query)
}

/**
* Create and return and un-saved model instance.
* Create and return an un-saved model instance.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public function __construct(Builder $query, Model $parent, $foreignKey, $localKe
parent::__construct($query, $parent);
}

/**
* Create and return an un-saved instance of the related model.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function make(array $attributes = [])
{
return tap($this->related->newInstance($attributes), function ($instance) {
$instance->setAttribute($this->getForeignKeyName(), $this->getParentKey());
});
Copy link
Member

Choose a reason for hiding this comment

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

setAttribute returns the model. Why do we need the tap call?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just copied the create method and removed the save call. You are right, the tap is not needed, as the $instance can be chained through the setAttribute and then returned.

}

/**
* Set the base constraints on the relation query.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ public function __construct(Builder $query, Model $parent, $type, $id, $localKey
parent::__construct($query, $parent, $id, $localKey);
}

/**
* Create and return an un-saved instance of the related model.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function make(array $attributes = [])
{
return tap($this->related->newInstance($attributes), function ($instance) {
// When saving a polymorphic relationship, we need to set not only the foreign
// key, but also the foreign key type, which is typically the class name of
// the parent model. This makes the polymorphic item unique in the table.
$this->setForeignAttributesForCreate($instance);
});
}

/**
* Set the base constraints on the relation query.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public function tearDown()
m::close();
}

public function testMakeMethodDoesNotSaveNewModel()
{
$relation = $this->getRelation();
$instance = $this->expectNewModel($relation, ['name' => 'taylor']);
$instance->expects($this->never())->method('save');

$this->assertEquals($instance, $relation->make(['name' => 'taylor']));
}

public function testCreateMethodProperlyCreatesNewModel()
{
$relation = $this->getRelation();
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabaseEloquentHasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ public function testHasOneWithArrayDefault()
$this->assertSame(1, $newModel->getAttribute('foreign_key'));
}

public function testMakeMethodDoesNotSaveNewModel()
{
$relation = $this->getRelation();
$instance = $this->getMockBuilder('Illuminate\Database\Eloquent\Model')->setMethods(['newInstance', 'setAttribute'])->getMock();
$relation->getRelated()->shouldReceive('newInstance')->with(['name' => 'taylor'])->andReturn($instance);
$instance->expects($this->once())->method('setAttribute')->with('foreign_key', 1);
$instance->expects($this->never())->method('save');

$this->assertEquals($instance, $relation->make(['name' => 'taylor']));
}

public function testSaveMethodSetsForeignKeyOnModel()
{
$relation = $this->getRelation();
Expand Down
14 changes: 14 additions & 0 deletions tests/Database/DatabaseEloquentMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ public function testMorphManyEagerConstraintsAreProperlyAdded()
$relation->addEagerConstraints([$model1, $model2]);
}

public function testMakeFunctionOnMorph()
{
$_SERVER['__eloquent.saved'] = false;
// Doesn't matter which relation type we use since they share the code...
$relation = $this->getOneRelation();
$instance = m::mock('Illuminate\Database\Eloquent\Model');
$instance->shouldReceive('setAttribute')->once()->with('morph_id', 1);
$instance->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
$instance->shouldReceive('save')->never();
$relation->getRelated()->shouldReceive('newInstance')->once()->with(['name' => 'taylor'])->andReturn($instance);

$this->assertEquals($instance, $relation->make(['name' => 'taylor']));
}

public function testCreateFunctionOnMorph()
{
// Doesn't matter which relation type we use since they share the code...
Expand Down