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

[10.x] Revival of the reverted changes in 10.25.0: firstOrCreate updateOrCreate improvement through createOrFirst + additional query tests #48637

Merged
merged 25 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7d793e4
Revert "[10.x] Revert from using `createOrFirst` in other `*OrCreate`…
mpyw Oct 5, 2023
495db28
test: 💍 Add `Builder::createOrFirst()` snapshot tests
mpyw Oct 5, 2023
ebc9ba1
test: 💍 Add `Builder::firstOrCreate()` snapshot tests
mpyw Oct 5, 2023
1eb9b1a
test: 💍 Add `Builder::updateOrCreate()` snapshot tests
mpyw Oct 5, 2023
ff45877
test: 💍 Add test stubs for `DatabaseEloquentHasManyTest`
mpyw Oct 5, 2023
9dad517
test: 💍 Add `HasMany::createOrFirst()` snapshot tests
mpyw Oct 5, 2023
de4c48c
test: 💍 Add `HasMany::firstOrCreate()` snapshot tests
mpyw Oct 5, 2023
1b2620c
test: 💍 Add `HasMany::updateOrCreate()` snapshot tests
mpyw Oct 5, 2023
02b1180
test: 💍 prepare HasManyThrough test
fuwasegu Oct 6, 2023
08beb98
test: 💍 Add HasManyThrough::CreateOrFirst() snapshot tests
fuwasegu Oct 6, 2023
b91d6e8
test: 💍 Add HasManyThrough::firstOrCreate() snapshot test
fuwasegu Oct 6, 2023
eee246b
test: 💍 Add HasManyThrough::updateOrCreate() snapshot test
fuwasegu Oct 6, 2023
15e8999
refactor: 💡 use createOrFirst in firstOrCreate
fuwasegu Oct 6, 2023
b79c5f0
test: 💍 fix test
fuwasegu Oct 6, 2023
7add83e
Merge pull request #1 from fuwasegu/test/has-many-through
mpyw Oct 6, 2023
30d4627
style: 💄 Apply StyleCI fixes
mpyw Oct 6, 2023
f42cd8b
docs: ✏️ Add missing FIXME comments
mpyw Oct 6, 2023
be2f795
refactor: 💡 Omit verbose arguments
mpyw Oct 6, 2023
77f2311
test: 💍 Rename `DatabaseEloquentHasManyThroughTest` with fixes
mpyw Oct 6, 2023
640d39c
test: 💍 Add `BelongsToMany::createOrFirst/firstOrCreate` tests
mpyw Oct 6, 2023
2935602
test: 💍 Extract `DatabaseEloquentHasManyTest` cases with fixes
mpyw Oct 6, 2023
2419b1c
test: 💍 Extract `DatabaseEloquentBuilderTest` cases with fixes
mpyw Oct 6, 2023
4cefa9c
test: 💍 refactoring
mpyw Oct 6, 2023
fab4ee3
test: 💍 Add `BelongsToMany::updateOrCreate` snapshot tests
mpyw Oct 6, 2023
19f8b61
Merge remote-tracking branch '10.x' into feat/create-or-first-optimiz…
mpyw Oct 10, 2023
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
8 changes: 5 additions & 3 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ public function firstOrCreate(array $attributes = [], array $values = [])
return $instance;
}

return $this->create(array_merge($attributes, $values));
return $this->createOrFirst($attributes, $values);
}

/**
Expand Down Expand Up @@ -595,8 +595,10 @@ public function createOrFirst(array $attributes = [], array $values = [])
*/
public function updateOrCreate(array $attributes, array $values = [])
{
return tap($this->firstOrNew($attributes), function ($instance) use ($values) {
$instance->fill($values)->save();
return tap($this->firstOrCreate($attributes, $values), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values)->save();
}
});
}

Expand Down
20 changes: 7 additions & 13 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ public function firstOrCreate(array $attributes = [], array $values = [], array
{
if (is_null($instance = (clone $this)->where($attributes)->first())) {
if (is_null($instance = $this->related->where($attributes)->first())) {
$instance = $this->create(array_merge($attributes, $values), $joining, $touch);
$instance = $this->createOrFirst($attributes, $values, $joining, $touch);
} else {
try {
$this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch));
Expand Down Expand Up @@ -672,19 +672,13 @@ public function createOrFirst(array $attributes = [], array $values = [], array
*/
public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true)
{
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);
return tap($this->firstOrCreate($attributes, $values, $joining, $touch), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values);

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

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

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public function firstOrCreate(array $attributes = [], array $values = [])
return $instance;
}

return $this->create(array_merge($attributes, $values));
return $this->createOrFirst(array_merge($attributes, $values));
}

/**
Expand Down Expand Up @@ -305,11 +305,11 @@ public function createOrFirst(array $attributes = [], array $values = [])
*/
public function updateOrCreate(array $attributes, array $values = [])
{
$instance = $this->firstOrNew($attributes);

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

return $instance;
return tap($this->firstOrCreate($attributes, $values), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values)->save();
}
});
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function firstOrNew(array $attributes = [], array $values = [])
public function firstOrCreate(array $attributes = [], array $values = [])
{
if (is_null($instance = $this->where($attributes)->first())) {
$instance = $this->create(array_merge($attributes, $values));
$instance = $this->createOrFirst($attributes, $values);
}

return $instance;
Expand Down Expand Up @@ -267,10 +267,10 @@ public function createOrFirst(array $attributes = [], array $values = [])
*/
public function updateOrCreate(array $attributes, array $values = [])
{
return tap($this->firstOrNew($attributes), function ($instance) use ($values) {
$instance->fill($values);

$instance->save();
return tap($this->firstOrCreate($attributes, $values), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values)->save();
}
});
}

Expand Down
Loading