Skip to content

Commit

Permalink
set relation parent key when using forceCreate (#42281)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra authored May 6, 2022
1 parent 934ec55 commit 0431475
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
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 @@ -297,6 +297,19 @@ public function create(array $attributes = [])
});
}

/**
* Create a new instance of the related model. Allow mass-assignment.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function forceCreate(array $attributes = [])
{
$attributes[$this->getForeignKeyName()] = $this->getParentKey();

return $this->related->forceCreate($attributes);
}

/**
* Create a Collection of new instances of the related model.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ public function testCreateMethodProperlyCreatesNewModel()
$this->assertEquals($created, $relation->create(['name' => 'taylor']));
}

public function testForceCreateMethodProperlyCreatesNewModel()
{
$relation = $this->getRelation();
$created = $this->expectForceCreatedModel($relation, ['name' => 'taylor']);

$this->assertEquals($created, $relation->forceCreate(['name' => 'taylor']));
$this->assertEquals(1, $created->getAttribute('foreign_key'));
}

public function testFindOrNewMethodFindsModel()
{
$relation = $this->getRelation();
Expand Down Expand Up @@ -304,6 +313,18 @@ protected function expectCreatedModel($relation, $attributes)

return $model;
}

protected function expectForceCreatedModel($relation, $attributes)
{
$attributes[$relation->getForeignKeyName()] = $relation->getParentKey();

$model = m::mock(Model::class);
$model->shouldReceive('getAttribute')->with($relation->getForeignKeyName())->andReturn($relation->getParentKey());

$relation->getRelated()->shouldReceive('forceCreate')->once()->with($attributes)->andReturn($model);

return $model;
}
}

class EloquentHasManyModelStub extends Model
Expand Down
14 changes: 14 additions & 0 deletions tests/Database/DatabaseEloquentHasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ public function testCreateMethodProperlyCreatesNewModel()
$this->assertEquals($created, $relation->create(['name' => 'taylor']));
}

public function testForceCreateMethodProperlyCreatesNewModel()
{
$relation = $this->getRelation();
$attributes = ['name' => 'taylor', $relation->getForeignKeyName() => $relation->getParentKey()];

$created = m::mock(Model::class);
$created->shouldReceive('getAttribute')->with($relation->getForeignKeyName())->andReturn($relation->getParentKey());

$relation->getRelated()->shouldReceive('forceCreate')->once()->with($attributes)->andReturn($created);

$this->assertEquals($created, $relation->forceCreate(['name' => 'taylor']));
$this->assertEquals(1, $created->getAttribute('foreign_key'));
}

public function testRelationIsProperlyInitialized()
{
$relation = $this->getRelation();
Expand Down

0 comments on commit 0431475

Please sign in to comment.