Skip to content

Commit

Permalink
Revert "[10.x] Make the updateOrCreate methods in relations use `fi…
Browse files Browse the repository at this point in the history
…rstOrCreate` behind the scenes (laravel#48213)"

This reverts commit e1ae507.
  • Loading branch information
tonysm committed Sep 25, 2023
1 parent f35c148 commit 85fedff
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
18 changes: 12 additions & 6 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,13 +672,19 @@ public function createOrFirst(array $attributes = [], array $values = [], array
*/
public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true)
{
return tap($this->firstOrCreate($attributes, $values, $joining, $touch), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values);

$instance->save(['touch' => false]);
if (is_null($instance = (clone $this)->where($attributes)->first())) {
if (is_null($instance = $this->related->where($attributes)->first())) {
return $this->create(array_merge($attributes, $values), $joining, $touch);
} else {
$this->attach($instance, $joining, $touch);
}
});
}

$instance->fill($values);

$instance->save(['touch' => false]);

return $instance;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ public function firstOrNew(array $attributes)
*/
public function updateOrCreate(array $attributes, array $values = [])
{
return tap($this->firstOrCreate($attributes, $values), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values)->save();
}
});
$instance = $this->firstOrNew($attributes);

$instance->fill($values)->save();

return $instance;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ public function createOrFirst(array $attributes = [], array $values = [])
*/
public function updateOrCreate(array $attributes, array $values = [])
{
return tap($this->firstOrCreate($attributes, $values), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values)->save();
}
return tap($this->firstOrNew($attributes), function ($instance) use ($values) {
$instance->fill($values);

$instance->save();
});
}

Expand Down
12 changes: 3 additions & 9 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
$relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(stdClass::class));
$relation->getRelated()->shouldReceive('newInstance')->never();

$model->wasRecentlyCreated = false;
$model->shouldReceive('fill')->once()->with(['bar'])->andReturn($model);
$model->shouldReceive('fill')->once()->with(['bar']);
$model->shouldReceive('save')->once();

$this->assertInstanceOf(stdClass::class, $relation->updateOrCreate(['foo'], ['bar']));
Expand All @@ -238,15 +236,11 @@ public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
public function testUpdateOrCreateMethodCreatesNewModelWithForeignKeySet()
{
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) {
return $scope();
});
$relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
$relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo', 'bar'])->andReturn($model = m::mock(Model::class));

$model->wasRecentlyCreated = true;
$relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo'])->andReturn($model = m::mock(Model::class));
$model->shouldReceive('save')->once()->andReturn(true);
$model->shouldReceive('fill')->once()->with(['bar']);
$model->shouldReceive('setAttribute')->once()->with('foreign_key', 1);

$this->assertInstanceOf(Model::class, $relation->updateOrCreate(['foo'], ['bar']));
Expand Down
12 changes: 3 additions & 9 deletions tests/Database/DatabaseEloquentMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,8 @@ public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
$relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));
$relation->getRelated()->shouldReceive('newInstance')->never();

$model->wasRecentlyCreated = false;
$model->shouldReceive('setAttribute')->never();
$model->shouldReceive('fill')->once()->with(['bar'])->andReturn($model);
$model->shouldReceive('fill')->once()->with(['bar']);
$model->shouldReceive('save')->once();

$this->assertInstanceOf(Model::class, $relation->updateOrCreate(['foo'], ['bar']));
Expand All @@ -314,17 +312,13 @@ public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
public function testUpdateOrCreateMethodCreatesNewMorphModel()
{
$relation = $this->getOneRelation();
$relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) {
return $scope();
});
$relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
$relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo', 'bar'])->andReturn($model = m::mock(Model::class));

$model->wasRecentlyCreated = true;
$relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo'])->andReturn($model = m::mock(Model::class));
$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);
$model->shouldReceive('fill')->once()->with(['bar']);

$this->assertInstanceOf(Model::class, $relation->updateOrCreate(['foo'], ['bar']));
}
Expand Down

0 comments on commit 85fedff

Please sign in to comment.