Skip to content

Commit c5bedea

Browse files
committed
PHPORM-171 Set timestamps when using createOrFirst
1 parent 8f7bf77 commit c5bedea

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Eloquent/Builder.php

+6
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ public function createOrFirst(array $attributes = [], array $values = []): Model
204204
// Apply casting and default values to the attributes
205205
// In case of duplicate key between the attributes and the values, the values have priority
206206
$instance = $this->newModelInstance($values + $attributes);
207+
208+
/* @see \Illuminate\Database\Eloquent\Model::performInsert */
209+
if ($instance->usesTimestamps()) {
210+
$instance->updateTimestamps();
211+
}
212+
207213
$values = $instance->getAttributes();
208214
$attributes = array_intersect_key($attributes, $values);
209215

tests/ModelTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Carbon\Carbon;
88
use DateTime;
9+
use DateTimeInterface;
910
use Generator;
1011
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
1112
use Illuminate\Database\Eloquent\ModelNotFoundException;
@@ -1048,12 +1049,19 @@ public function testNumericFieldName(): void
10481049

10491050
public function testCreateOrFirst()
10501051
{
1052+
Carbon::setTestNow('2010-06-22');
1053+
$createdAt = Carbon::now()->getTimestamp();
10511054
$user1 = User::createOrFirst(['email' => '[email protected]']);
10521055

10531056
$this->assertSame('[email protected]', $user1->email);
10541057
$this->assertNull($user1->name);
10551058
$this->assertTrue($user1->wasRecentlyCreated);
1059+
$this->assertInstanceOf(DateTimeInterface::class, $user1->created_at);
1060+
$this->assertInstanceOf(DateTimeInterface::class, $user1->updated_at);
1061+
$this->assertEquals($createdAt, $user1->created_at->getTimestamp());
1062+
$this->assertEquals($createdAt, $user1->updated_at->getTimestamp());
10561063

1064+
Carbon::setTestNow('2020-12-28');
10571065
$user2 = User::createOrFirst(
10581066
['email' => '[email protected]'],
10591067
['name' => 'John Doe', 'birthday' => new DateTime('1987-05-28')],
@@ -1064,6 +1072,10 @@ public function testCreateOrFirst()
10641072
$this->assertNull($user2->name);
10651073
$this->assertNull($user2->birthday);
10661074
$this->assertFalse($user2->wasRecentlyCreated);
1075+
$this->assertInstanceOf(DateTimeInterface::class, $user2->created_at);
1076+
$this->assertInstanceOf(DateTimeInterface::class, $user2->updated_at);
1077+
$this->assertEquals($createdAt, $user1->created_at->getTimestamp());
1078+
$this->assertEquals($createdAt, $user1->updated_at->getTimestamp());
10671079

10681080
$user3 = User::createOrFirst(
10691081
['email' => '[email protected]'],
@@ -1075,6 +1087,10 @@ public function testCreateOrFirst()
10751087
$this->assertSame('Jane Doe', $user3->name);
10761088
$this->assertEquals(new DateTime('1987-05-28'), $user3->birthday);
10771089
$this->assertTrue($user3->wasRecentlyCreated);
1090+
$this->assertInstanceOf(DateTimeInterface::class, $user3->created_at);
1091+
$this->assertInstanceOf(DateTimeInterface::class, $user3->updated_at);
1092+
$this->assertEquals($createdAt, $user1->created_at->getTimestamp());
1093+
$this->assertEquals($createdAt, $user1->updated_at->getTimestamp());
10781094

10791095
$user4 = User::createOrFirst(
10801096
['name' => 'Robert Doe'],

0 commit comments

Comments
 (0)