From 52b9f673bb98c165836fd88f0f12fffa8423a083 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Tue, 29 Aug 2023 11:47:47 +0200 Subject: [PATCH 1/2] wip --- src/Illuminate/Database/Eloquent/Builder.php | 1 + .../Database/MySql/EloquentCastTest.php | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 02a5d60eef16..b6beb1713fa5 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -1167,6 +1167,7 @@ protected function addUpdatedAtColumn(array $values) $timestamp = $this->model->newInstance() ->forceFill([$column => $timestamp]) ->getAttributes()[$column]; + // ->getAttributes()[$column] ?? $timestamp; } $values = array_merge([$column => $timestamp], $values); diff --git a/tests/Integration/Database/MySql/EloquentCastTest.php b/tests/Integration/Database/MySql/EloquentCastTest.php index 42a6d07b240c..9c45838d59d0 100644 --- a/tests/Integration/Database/MySql/EloquentCastTest.php +++ b/tests/Integration/Database/MySql/EloquentCastTest.php @@ -115,6 +115,28 @@ public function testItCastTimestampsCreatedByTheBuilderWhenTimeHasPassed() $this->assertSame($updatedAt, $mutatorUser->updated_at->timestamp); $this->assertSame($updatedAt, $mutatorUser->fresh()->updated_at->timestamp); } + + public function testItCastTimestampsUpdatedByAMutator() + { + Carbon::setTestNow(now()); + $createdAt = now()->timestamp; + + $mutatorUser = UserWithUpdatedAtViaMutator::create([ + 'email' => fake()->unique()->email, + ]); + + $this->assertSame($createdAt, $mutatorUser->updated_at->timestamp); + + Carbon::setTestNow(now()->addSecond()); + $updatedAt = now()->timestamp; + + $mutatorUser->update([ + 'email' => fake()->unique()->email, + ]); + + $this->assertSame($updatedAt, $mutatorUser->updated_at->timestamp); + $this->assertSame($updatedAt, $mutatorUser->fresh()->updated_at->timestamp); + } } class UserWithIntTimestampsViaCasts extends Model @@ -191,3 +213,34 @@ protected function setCreatedAtAttribute($value) $this->attributes['created_at'] = Carbon::parse($value)->timestamp; } } + +class UserWithUpdatedAtViaMutator extends Model +{ + protected $table = 'users'; + + protected $fillable = ['email']; + + protected function getUpdatedAtAttribute($value) + { + return Carbon::parse($value); + } + + public function setUpdatedAtAttribute($value) + { + if (! $this->id) { + return; + } + + $this->updated_at = $value; + } + + protected function getCreatedAtAttribute($value) + { + return Carbon::parse($value); + } + + protected function setCreatedAtAttribute($value) + { + $this->attributes['created_at'] = Carbon::parse($value)->timestamp; + } +} From 7cd84fd3020f1d79c923ae78be362fe5cbb42b73 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Tue, 29 Aug 2023 15:03:43 +0200 Subject: [PATCH 2/2] wip --- src/Illuminate/Database/Eloquent/Builder.php | 3 +- .../Database/MySql/EloquentCastTest.php | 29 +++++++------------ 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index b6beb1713fa5..c5ab6b6790e9 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -1166,8 +1166,7 @@ protected function addUpdatedAtColumn(array $values) ) { $timestamp = $this->model->newInstance() ->forceFill([$column => $timestamp]) - ->getAttributes()[$column]; - // ->getAttributes()[$column] ?? $timestamp; + ->getAttributes()[$column] ?? $timestamp; } $values = array_merge([$column => $timestamp], $values); diff --git a/tests/Integration/Database/MySql/EloquentCastTest.php b/tests/Integration/Database/MySql/EloquentCastTest.php index 9c45838d59d0..651a1e6711da 100644 --- a/tests/Integration/Database/MySql/EloquentCastTest.php +++ b/tests/Integration/Database/MySql/EloquentCastTest.php @@ -20,6 +20,13 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed() $table->integer('created_at'); $table->integer('updated_at'); }); + + Schema::create('users_nullable_timestamps', function ($table) { + $table->increments('id'); + $table->string('email')->unique(); + $table->timestamp('created_at')->nullable(); + $table->timestamp('updated_at')->nullable(); + }); } protected function destroyDatabaseMigrations() @@ -119,13 +126,12 @@ public function testItCastTimestampsCreatedByTheBuilderWhenTimeHasPassed() public function testItCastTimestampsUpdatedByAMutator() { Carbon::setTestNow(now()); - $createdAt = now()->timestamp; $mutatorUser = UserWithUpdatedAtViaMutator::create([ 'email' => fake()->unique()->email, ]); - $this->assertSame($createdAt, $mutatorUser->updated_at->timestamp); + $this->assertNull($mutatorUser->updated_at); Carbon::setTestNow(now()->addSecond()); $updatedAt = now()->timestamp; @@ -216,14 +222,9 @@ protected function setCreatedAtAttribute($value) class UserWithUpdatedAtViaMutator extends Model { - protected $table = 'users'; + protected $table = 'users_nullable_timestamps'; - protected $fillable = ['email']; - - protected function getUpdatedAtAttribute($value) - { - return Carbon::parse($value); - } + protected $fillable = ['email', 'updated_at']; public function setUpdatedAtAttribute($value) { @@ -233,14 +234,4 @@ public function setUpdatedAtAttribute($value) $this->updated_at = $value; } - - protected function getCreatedAtAttribute($value) - { - return Carbon::parse($value); - } - - protected function setCreatedAtAttribute($value) - { - $this->attributes['created_at'] = Carbon::parse($value)->timestamp; - } }