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.5] Add support for additional values in relationship firstOrCreate and firstOrNew #18878

Merged
merged 1 commit into from
Apr 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
10 changes: 6 additions & 4 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,13 @@ public function findOrNew($id, $columns = ['*'])
* Get the first related model record matching the attributes or instantiate it.
*
* @param array $attributes
* @param array $values
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrNew(array $attributes)
public function firstOrNew(array $attributes, array $values = [])
{
if (is_null($instance = $this->where($attributes)->first())) {
$instance = $this->related->newInstance($attributes);
$instance = $this->related->newInstance($attributes + $values);

$instance->setAttribute($this->getForeignKeyName(), $this->getParentKey());
}
Expand All @@ -202,12 +203,13 @@ public function firstOrNew(array $attributes)
* Get the first related record matching the attributes or create it.
*
* @param array $attributes
* @param array $values
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrCreate(array $attributes)
public function firstOrCreate(array $attributes, array $values = [])
{
if (is_null($instance = $this->where($attributes)->first())) {
$instance = $this->create($attributes);
$instance = $this->create($attributes + $values);
}

return $instance;
Expand Down
10 changes: 6 additions & 4 deletions src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ public function findOrNew($id, $columns = ['*'])
* Get the first related model record matching the attributes or instantiate it.
*
* @param array $attributes
* @param array $values
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrNew(array $attributes)
public function firstOrNew(array $attributes, array $values = [])
{
if (is_null($instance = $this->where($attributes)->first())) {
$instance = $this->related->newInstance($attributes);
$instance = $this->related->newInstance($attributes + $values);

// 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
Expand All @@ -112,12 +113,13 @@ public function firstOrNew(array $attributes)
* Get the first related record matching the attributes or create it.
*
* @param array $attributes
* @param array $values
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrCreate(array $attributes)
public function firstOrCreate(array $attributes, array $values = [])
{
if (is_null($instance = $this->where($attributes)->first())) {
$instance = $this->create($attributes);
$instance = $this->create($attributes + $values);
}

return $instance;
Expand Down
43 changes: 43 additions & 0 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ public function testFirstOrNewMethodFindsFirstModel()
$this->assertInstanceOf(StdClass::class, $relation->firstOrNew(['foo']));
}

public function testFirstOrNewMethodWithValuesFindsFirstModel()
{
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock('StdClass'));
$relation->getRelated()->shouldReceive('newInstance')->never();
$model->shouldReceive('setAttribute')->never();

$this->assertInstanceOf(StdClass::class, $relation->firstOrNew(['foo' => 'bar'], ['baz' => 'qux']));
}

public function testFirstOrNewMethodReturnsNewModelWithForeignKeySet()
{
$relation = $this->getRelation();
Expand All @@ -62,6 +73,16 @@ public function testFirstOrNewMethodReturnsNewModelWithForeignKeySet()
$this->assertEquals($model, $relation->firstOrNew(['foo']));
}

public function testFirstOrNewMethodWithValuesCreatesNewModelWithForeignKeySet()
{
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
$model = $this->expectNewModel($relation, ['foo' => 'bar', 'baz' => 'qux']);

$this->assertEquals($model, $relation->firstOrNew(['foo' => 'bar'], ['baz' => 'qux']));
}

public function testFirstOrCreateMethodFindsFirstModel()
{
$relation = $this->getRelation();
Expand All @@ -74,6 +95,18 @@ public function testFirstOrCreateMethodFindsFirstModel()
$this->assertInstanceOf(StdClass::class, $relation->firstOrCreate(['foo']));
}

public function testFirstOrCreateMethodWithValuesFindsFirstModel()
{
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock('StdClass'));
$relation->getRelated()->shouldReceive('newInstance')->never();
$model->shouldReceive('setAttribute')->never();
$model->shouldReceive('save')->never();

$this->assertInstanceOf(StdClass::class, $relation->firstOrCreate(['foo' => 'bar'], ['baz' => 'qux']));
}

public function testFirstOrCreateMethodCreatesNewModelWithForeignKeySet()
{
$relation = $this->getRelation();
Expand All @@ -84,6 +117,16 @@ public function testFirstOrCreateMethodCreatesNewModelWithForeignKeySet()
$this->assertEquals($model, $relation->firstOrCreate(['foo']));
}

public function testFirstOrCreateMethodWithValuesCreatesNewModelWithForeignKeySet()
{
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
$model = $this->expectCreatedModel($relation, ['foo' => 'bar', 'baz' => 'qux']);

$this->assertEquals($model, $relation->firstOrCreate(['foo' => 'bar'], ['baz' => 'qux']));
}

public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
{
$relation = $this->getRelation();
Expand Down
50 changes: 50 additions & 0 deletions tests/Database/DatabaseEloquentMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ public function testFirstOrNewMethodFindsFirstModel()
$this->assertInstanceOf(Model::class, $relation->firstOrNew(['foo']));
}

public function testFirstOrNewMethodWithValueFindsFirstModel()
{
$relation = $this->getOneRelation();
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock('Illuminate\Database\Eloquent\Model'));
$relation->getRelated()->shouldReceive('newInstance')->never();
$model->shouldReceive('setAttribute')->never();
$model->shouldReceive('save')->never();

$this->assertInstanceOf(Model::class, $relation->firstOrNew(['foo' => 'bar'], ['baz' => 'qux']));
}

public function testFirstOrNewMethodReturnsNewModelWithMorphKeysSet()
{
$relation = $this->getOneRelation();
Expand All @@ -119,6 +131,19 @@ public function testFirstOrNewMethodReturnsNewModelWithMorphKeysSet()
$this->assertInstanceOf(Model::class, $relation->firstOrNew(['foo']));
}

public function testFirstOrNewMethodWithValuesReturnsNewModelWithMorphKeysSet()
{
$relation = $this->getOneRelation();
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
$relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo' => 'bar', 'baz' => 'qux'])->andReturn($model = m::mock('Illuminate\Database\Eloquent\Model'));
$model->shouldReceive('setAttribute')->once()->with('morph_id', 1);
$model->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
$model->shouldReceive('save')->never();

$this->assertInstanceOf(Model::class, $relation->firstOrNew(['foo' => 'bar'], ['baz' => 'qux']));
}

public function testFirstOrCreateMethodFindsFirstModel()
{
$relation = $this->getOneRelation();
Expand All @@ -131,6 +156,18 @@ public function testFirstOrCreateMethodFindsFirstModel()
$this->assertInstanceOf(Model::class, $relation->firstOrCreate(['foo']));
}

public function testFirstOrCreateMethodWithValuesFindsFirstModel()
{
$relation = $this->getOneRelation();
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock('Illuminate\Database\Eloquent\Model'));
$relation->getRelated()->shouldReceive('newInstance')->never();
$model->shouldReceive('setAttribute')->never();
$model->shouldReceive('save')->never();

$this->assertInstanceOf(Model::class, $relation->firstOrCreate(['foo' => 'bar'], ['baz' => 'qux']));
}

public function testFirstOrCreateMethodCreatesNewMorphModel()
{
$relation = $this->getOneRelation();
Expand All @@ -144,6 +181,19 @@ public function testFirstOrCreateMethodCreatesNewMorphModel()
$this->assertInstanceOf(Model::class, $relation->firstOrCreate(['foo']));
}

public function testFirstOrCreateMethodWithValuesCreatesNewMorphModel()
{
$relation = $this->getOneRelation();
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
$relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo' => 'bar', 'baz' => 'qux'])->andReturn($model = m::mock('Illuminate\Database\Eloquent\Model'));
$model->shouldReceive('setAttribute')->once()->with('morph_id', 1);
$model->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
$model->shouldReceive('save')->once()->andReturn(true);

$this->assertInstanceOf(Model::class, $relation->firstOrCreate(['foo' => 'bar'], ['baz' => 'qux']));
}

public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
{
$relation = $this->getOneRelation();
Expand Down