Skip to content

Commit 06f2da3

Browse files
committed
welcome hydrateAndInsert
1 parent ab5d25e commit 06f2da3

File tree

2 files changed

+56
-51
lines changed

2 files changed

+56
-51
lines changed

src/Illuminate/Database/Eloquent/Builder.php

+19-45
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ class Builder implements BuilderContract
6060
*/
6161
public $pendingAttributes = [];
6262

63-
/**
64-
* Indicates if attributes, timestamps, and unique IDs should be merged before insert.
65-
*
66-
* @var bool
67-
*/
68-
public $mergeAttributesBeforeInsert = false;
69-
7063
/**
7164
* The relationships that should be eager loaded.
7265
*
@@ -596,56 +589,51 @@ public function findOr($id, $columns = ['*'], ?Closure $callback = null)
596589
}
597590

598591
/**
599-
* Insert new records into the database.
592+
* Merge in Model's default attributes, set timestamps,
593+
* cast any values, and then insert into the database.
600594
*
595+
* @param array<int, array<string, mixed>> $values
601596
* @return bool
602597
*/
603-
public function insert(array $values)
598+
public function hydrateAndInsert(array $values)
604599
{
605-
if ($this->mergeAttributesBeforeInsert) {
606-
$values = $this->castBeforeInsert($values);
607-
}
608-
609-
return $this->forwardCallTo($this->query, 'insert', [$values]);
600+
return $this->insert($this->hydrateForInsert($values));
610601
}
611602

612603
/**
613-
* Insert a new record and get the value of the primary key.
604+
* Merge in Model's default attributes, set timestamps,
605+
* cast any values, and then insert into the database,
606+
* ignoring errors.
614607
*
615-
* @param string|null $sequence
608+
* @param array<int, array<string, mixed>> $values
616609
* @return int
617610
*/
618-
public function insertGetId(array $values, $sequence = null)
611+
public function hydrateAndInsertOrIgnore(array $values)
619612
{
620-
if ($this->mergeAttributesBeforeInsert) {
621-
$values = $this->castBeforeInsert([$values])[0];
622-
}
623-
624-
return $this->forwardCallTo($this->query, 'insertGetId', [$values, $sequence]);
613+
return $this->insertOrIgnore($this->hydrateForInsert($values));
625614
}
626615

627616
/**
628-
* Insert new records into the database while ignoring errors.
617+
* Merge in Model's default attributes, set timestamps,
618+
* cast any values, and then insert into the database,
619+
* returning the ID of the new record.
629620
*
621+
* @param array<string, mixed> $values
630622
* @return int
631623
*/
632-
public function insertOrIgnore(array $values)
624+
public function hydrateAndInsertGetId(array $values)
633625
{
634-
if ($this->mergeAttributesBeforeInsert) {
635-
$values = $this->castBeforeInsert($values);
636-
}
637-
638-
return $this->forwardCallTo($this->query, 'insertOrIgnore', [$values]);
626+
return $this->insertGetId($this->hydrateForInsert([$values])[0]);
639627
}
640628

641629
/**
642-
* Insert a number of records, merging in default attributes,
630+
* Enrich values by merging in the Model's default attributes,
643631
* adding timestamps, and converting casts to raw values.
644632
*
645633
* @param array<int, array<string, mixed>> $values
646634
* @return array<int, array<string, mixed>>
647635
*/
648-
public function castBeforeInsert(array $values)
636+
public function hydrateForInsert(array $values)
649637
{
650638
if (empty($values)) {
651639
return [];
@@ -1915,20 +1903,6 @@ public function withAttributes(Expression|array|string $attributes, $value = nul
19151903
return $this;
19161904
}
19171905

1918-
/**
1919-
* Indicate if insert methods should merge in default attributes,
1920-
* add timestamps, and converting casts to raw values.
1921-
*
1922-
* @param bool $mergeBeforeInsert
1923-
* @return $this
1924-
*/
1925-
public function mergeAttributesBeforeInsert($mergeBeforeInsert = true)
1926-
{
1927-
$this->mergeAttributesBeforeInsert = $mergeBeforeInsert;
1928-
1929-
return $this;
1930-
}
1931-
19321906
/**
19331907
* Apply query-time casts to the model instance.
19341908
*

tests/Database/DatabaseEloquentIntegrationTest.php

+37-6
Original file line numberDiff line numberDiff line change
@@ -2474,12 +2474,12 @@ public function testTouchingBiDirectionalChaperonedModelUpdatesAllRelatedTimesta
24742474
}
24752475
}
24762476

2477-
public function testCanInsertWithCasts()
2477+
public function testCanEnrichAndInsert()
24782478
{
24792479
DB::enableQueryLog();
24802480
Carbon::setTestNow('2025-03-15T07:32:00Z');
24812481

2482-
$this->assertTrue(EloquentTestUser::mergeAttributesBeforeInsert()->insert([
2482+
$this->assertTrue(EloquentTestUser::hydrateAndInsert([
24832483
['email' => '[email protected]', 'birthday' => null],
24842484
['email' => '[email protected]', 'birthday' => new Carbon('1980-01-01')],
24852485
['email' => '[email protected]', 'birthday' => '1987-11-01', 'created_at' => '2025-01-02T02:00:55', 'updated_at' => Carbon::parse('2025-02-19T11:41:13')],
@@ -2505,7 +2505,7 @@ public function testCanInsertWithCasts()
25052505

25062506
DB::flushQueryLog();
25072507

2508-
$this->assertTrue(EloquentTestWithJSON::mergeAttributesBeforeInsert()->insert([
2508+
$this->assertTrue(EloquentTestWithJSON::hydrateAndInsert([
25092509
['id' => 1, 'json' => ['album' => 'Keep It Like a Secret', 'release_date' => '1999-02-02']],
25102510
['id' => 2, 'json' => (object) ['album' => 'You In Reverse', 'release_date' => '2006-04-11']],
25112511
]));
@@ -2520,15 +2520,15 @@ public function testCanInsertWithCasts()
25202520
});
25212521
}
25222522

2523-
public function testCanInsertWithCastsWithUniqueStringIds()
2523+
public function testCanHydrateAndInsertWithUniqueStringIds()
25242524
{
25252525
Str::createUuidsUsingSequence([
25262526
'00000000-0000-7000-0000-000000000000',
25272527
'11111111-0000-7000-0000-000000000000',
25282528
'22222222-0000-7000-0000-000000000000',
25292529
]);
25302530

2531-
$this->assertTrue(ModelWithUniqueStringIds::mergeAttributesBeforeInsert()->insert([
2531+
$this->assertTrue(ModelWithUniqueStringIds::hydrateAndInsert([
25322532
[
25332533
'name' => 'Taylor', 'role' => IntBackedRole::Admin, 'role_string' => StringBackedRole::Admin,
25342534
],
@@ -2567,6 +2567,37 @@ public function testCanInsertWithCastsWithUniqueStringIds()
25672567
$this->assertSame('22222222-0000-7000-0000-000000000000', $chris->uuid);
25682568
}
25692569

2570+
public function testHydrateAndInsertOrIgnore()
2571+
{
2572+
Str::createUuidsUsingSequence([
2573+
'00000000-0000-7000-0000-000000000000',
2574+
'11111111-0000-7000-0000-000000000000',
2575+
'22222222-0000-7000-0000-000000000000',
2576+
]);
2577+
2578+
$this->assertEquals(1, ModelWithUniqueStringIds::hydrateAndInsertOrIgnore([
2579+
[
2580+
'id' => 1, 'name' => 'Taylor', 'role' => IntBackedRole::Admin, 'role_string' => StringBackedRole::Admin,
2581+
],
2582+
]));
2583+
2584+
$this->assertSame(1, ModelWithUniqueStringIds::hydrateAndInsertOrIgnore([
2585+
[
2586+
'id' => 1, 'name' => 'Taylor', 'role' => IntBackedRole::Admin, 'role_string' => StringBackedRole::Admin,
2587+
],
2588+
[
2589+
'id' => 2, 'name' => 'Nuno',
2590+
],
2591+
]));
2592+
2593+
$models = ModelWithUniqueStringIds::get();
2594+
$this->assertSame('00000000-0000-7000-0000-000000000000', $models->firstWhere('name', 'Taylor')->uuid);
2595+
$this->assertSame(
2596+
['uuid' => '22222222-0000-7000-0000-000000000000', 'role' => IntBackedRole::User],
2597+
$models->firstWhere('name', 'Nuno')->only('uuid', 'role')
2598+
);
2599+
}
2600+
25702601
public function testMergeBeforeInsertGetId()
25712602
{
25722603
Str::createUuidsUsingSequence([
@@ -2575,7 +2606,7 @@ public function testMergeBeforeInsertGetId()
25752606

25762607
DB::enableQueryLog();
25772608

2578-
$this->assertIsInt($newId = ModelWithUniqueStringIds::mergeAttributesBeforeInsert()->insertGetId([
2609+
$this->assertIsInt($newId = ModelWithUniqueStringIds::hydrateAndinsertGetId([
25792610
'name' => 'Taylor',
25802611
'role' => IntBackedRole::Admin,
25812612
'role_string' => StringBackedRole::Admin,

0 commit comments

Comments
 (0)