Skip to content

PHPORM-171 Set timestamps when using createOrFirst #2905

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

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ public function createOrFirst(array $attributes = [], array $values = []): Model
// Apply casting and default values to the attributes
// In case of duplicate key between the attributes and the values, the values have priority
$instance = $this->newModelInstance($values + $attributes);

/* @see \Illuminate\Database\Eloquent\Model::performInsert */
if ($instance->usesTimestamps()) {
$instance->updateTimestamps();
}
Comment on lines +207 to +211
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


$values = $instance->getAttributes();
$attributes = array_intersect_key($attributes, $values);

Expand Down
9 changes: 9 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1048,12 +1048,17 @@ public function testNumericFieldName(): void

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

$this->assertSame('[email protected]', $user1->email);
$this->assertNull($user1->name);
$this->assertTrue($user1->wasRecentlyCreated);
$this->assertEquals($createdAt, $user1->created_at->getTimestamp());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use a comparison to assert that the created_at and updated_at fields are greater than or equal to the previously calculated current time?

I don't see anything in the PR that would guarantee the entire test completes within a single timestamp.

Copy link
Member Author

@GromNaN GromNaN Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Carbon::setTestNow freezes the clock, so that the date point that is used to populate create_at is the one that is set.

$this->assertEquals($createdAt, $user1->updated_at->getTimestamp());

Carbon::setTestNow('2020-12-28');
$user2 = User::createOrFirst(
['email' => '[email protected]'],
['name' => 'John Doe', 'birthday' => new DateTime('1987-05-28')],
Expand All @@ -1064,6 +1069,8 @@ public function testCreateOrFirst()
$this->assertNull($user2->name);
$this->assertNull($user2->birthday);
$this->assertFalse($user2->wasRecentlyCreated);
$this->assertEquals($createdAt, $user1->created_at->getTimestamp());
$this->assertEquals($createdAt, $user1->updated_at->getTimestamp());

$user3 = User::createOrFirst(
['email' => '[email protected]'],
Expand All @@ -1075,6 +1082,8 @@ public function testCreateOrFirst()
$this->assertSame('Jane Doe', $user3->name);
$this->assertEquals(new DateTime('1987-05-28'), $user3->birthday);
$this->assertTrue($user3->wasRecentlyCreated);
$this->assertEquals($createdAt, $user1->created_at->getTimestamp());
$this->assertEquals($createdAt, $user1->updated_at->getTimestamp());

$user4 = User::createOrFirst(
['name' => 'Robert Doe'],
Expand Down
Loading