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] Use model cast when builder created updated at value #47942

Merged
merged 4 commits into from Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/Illuminate/Database/Eloquent/Builder.php
Expand Up @@ -1139,10 +1139,19 @@ protected function addUpdatedAtColumn(array $values)

$column = $this->model->getUpdatedAtColumn();

$values = array_merge(
[$column => $this->model->freshTimestampString()],
$values
);
if (! array_key_exists($column, $values)) {
$timestamp = $this->model->freshTimestampString();

if (
$this->model->hasSetMutator($column)
|| $this->model->hasAttributeSetMutator($column)
|| $this->model->hasCast($column)
) {
$timestamp = $this->model->newInstance([$column => $timestamp])->getAttributes()[$column];
}

$values = array_merge([$column => $timestamp], $values);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

The outer array_key_exists check is a micro optimisation here, as per the performance section of the description. It can be safely removed if we don't want it.


$segments = preg_split('/\s+as\s+/i', $this->query->from);

Expand Down
193 changes: 193 additions & 0 deletions tests/Integration/Database/MySql/EloquentCastTest.php
@@ -0,0 +1,193 @@
<?php

namespace Illuminate\Tests\Integration\Database\MySql;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Schema;

class EloquentCastTest extends MySqlTestCase
{
protected $driver = 'mysql';

protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
{
Schema::create('users', function ($table) {
$table->increments('id');
$table->string('email')->unique();
$table->integer('created_at');
$table->integer('updated_at');
});
}

protected function destroyDatabaseMigrations()
{
Schema::drop('users');
}

public function testItCastTimestampsCreatedByTheBuilderWhenTimeHasNotPassed()
{
Carbon::setTestNow(now());
$createdAt = now()->timestamp;

$castUser = UserWithIntTimestampsViaCasts::create([
'email' => fake()->unique()->email,
]);
$attributeUser = UserWithIntTimestampsViaAttribute::create([
'email' => fake()->unique()->email,
]);
$mutatorUser = UserWithIntTimestampsViaMutator::create([
'email' => fake()->unique()->email,
]);

$this->assertSame($createdAt, $castUser->created_at->timestamp);
$this->assertSame($createdAt, $castUser->updated_at->timestamp);
$this->assertSame($createdAt, $attributeUser->created_at->timestamp);
$this->assertSame($createdAt, $attributeUser->updated_at->timestamp);
$this->assertSame($createdAt, $mutatorUser->created_at->timestamp);
$this->assertSame($createdAt, $mutatorUser->updated_at->timestamp);

$castUser->update([
'email' => fake()->unique()->email,
]);
$attributeUser->update([
'email' => fake()->unique()->email,
]);
$mutatorUser->update([
'email' => fake()->unique()->email,
]);

$this->assertSame($createdAt, $castUser->created_at->timestamp);
$this->assertSame($createdAt, $castUser->updated_at->timestamp);
$this->assertSame($createdAt, $castUser->fresh()->updated_at->timestamp);
$this->assertSame($createdAt, $attributeUser->created_at->timestamp);
$this->assertSame($createdAt, $attributeUser->updated_at->timestamp);
$this->assertSame($createdAt, $attributeUser->fresh()->updated_at->timestamp);
$this->assertSame($createdAt, $mutatorUser->created_at->timestamp);
$this->assertSame($createdAt, $mutatorUser->updated_at->timestamp);
$this->assertSame($createdAt, $mutatorUser->fresh()->updated_at->timestamp);
}

public function testItCastTimestampsCreatedByTheBuilderWhenTimeHasPassed()
{
Carbon::setTestNow(now());
$createdAt = now()->timestamp;

$castUser = UserWithIntTimestampsViaCasts::create([
'email' => fake()->unique()->email,
]);
$attributeUser = UserWithIntTimestampsViaAttribute::create([
'email' => fake()->unique()->email,
]);
$mutatorUser = UserWithIntTimestampsViaMutator::create([
'email' => fake()->unique()->email,
]);

$this->assertSame($createdAt, $castUser->created_at->timestamp);
$this->assertSame($createdAt, $castUser->updated_at->timestamp);
$this->assertSame($createdAt, $attributeUser->created_at->timestamp);
$this->assertSame($createdAt, $attributeUser->updated_at->timestamp);
$this->assertSame($createdAt, $mutatorUser->created_at->timestamp);
$this->assertSame($createdAt, $mutatorUser->updated_at->timestamp);

Carbon::setTestNow(now()->addSecond());
$updatedAt = now()->timestamp;

$castUser->update([
'email' => fake()->unique()->email,
]);
$attributeUser->update([
'email' => fake()->unique()->email,
]);
$mutatorUser->update([
'email' => fake()->unique()->email,
]);

$this->assertSame($createdAt, $castUser->created_at->timestamp);
$this->assertSame($updatedAt, $castUser->updated_at->timestamp);
$this->assertSame($updatedAt, $castUser->fresh()->updated_at->timestamp);
$this->assertSame($createdAt, $attributeUser->created_at->timestamp);
$this->assertSame($updatedAt, $attributeUser->updated_at->timestamp);
$this->assertSame($updatedAt, $attributeUser->fresh()->updated_at->timestamp);
$this->assertSame($createdAt, $mutatorUser->created_at->timestamp);
$this->assertSame($updatedAt, $mutatorUser->updated_at->timestamp);
$this->assertSame($updatedAt, $mutatorUser->fresh()->updated_at->timestamp);
}
}

class UserWithIntTimestampsViaCasts extends Model
{
protected $table = 'users';

protected $guarded = [];

protected $casts = [
'created_at' => UnixTimeStampToCarbon::class,
'updated_at' => UnixTimeStampToCarbon::class,
];
}

class UnixTimeStampToCarbon implements CastsAttributes
{
public function get($model, string $key, $value, array $attributes)
{
return Carbon::parse($value);
}

public function set($model, string $key, $value, array $attributes)
{
return Carbon::parse($value)->timestamp;
}
}

class UserWithIntTimestampsViaAttribute extends Model
{
protected $table = 'users';

protected $guarded = [];

protected function updatedAt(): Attribute
{
return Attribute::make(
get: fn ($value) => Carbon::parse($value),
set: fn ($value) => Carbon::parse($value)->timestamp,
);
}

protected function createdAt(): Attribute
{
return Attribute::make(
get: fn ($value) => Carbon::parse($value),
set: fn ($value) => Carbon::parse($value)->timestamp,
);
}
}

class UserWithIntTimestampsViaMutator extends Model
{
protected $table = 'users';

protected $guarded = [];

protected function getUpdatedAtAttribute($value)
{
return Carbon::parse($value);
}

protected function setUpdatedAtAttribute($value)
{
$this->attributes['updated_at'] = Carbon::parse($value)->timestamp;
}

protected function getCreatedAtAttribute($value)
{
return Carbon::parse($value);
}

protected function setCreatedAtAttribute($value)
{
$this->attributes['created_at'] = Carbon::parse($value)->timestamp;
}
}