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

[8.x] - fix for custom date castable and database value formatting #38994

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
13 changes: 12 additions & 1 deletion src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,17 @@ protected function isDateCastable($key)
return $this->hasCast($key, ['date', 'datetime', 'immutable_date', 'immutable_datetime']);
}

/**
* Determine whether a value is Date / DateTime custom-castable for inbound manipulation.
*
* @param string $key
* @return bool
*/
protected function isDateCastableWithCustomFormat($key)
{
return $this->hasCast($key, ['custom_datetime', 'immutable_custom_datetime']);
}

/**
* Determine whether a value is JSON castable for inbound manipulation.
*
Expand Down Expand Up @@ -1645,7 +1656,7 @@ public function originalIsEquivalent($key)
return true;
} elseif (is_null($attribute)) {
return false;
} elseif ($this->isDateAttribute($key)) {
} elseif ($this->isDateAttribute($key) || $this->isDateCastableWithCustomFormat($key)) {
return $this->fromDateTime($attribute) ===
$this->fromDateTime($original);
} elseif ($this->hasCast($key, ['object', 'collection'])) {
Expand Down
88 changes: 87 additions & 1 deletion tests/Integration/Database/EloquentModelDateCastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Illuminate\Tests\Integration\Database\EloquentModelDateCastingTest;

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;

Expand All @@ -21,6 +22,8 @@ protected function setUp(): void
$table->increments('id');
$table->date('date_field')->nullable();
$table->datetime('datetime_field')->nullable();
$table->date('immutable_date_field')->nullable();
$table->datetime('immutable_datetime_field')->nullable();
});
}

Expand All @@ -36,6 +39,87 @@ public function testDatesAreCustomCastable()
$this->assertInstanceOf(Carbon::class, $user->date_field);
$this->assertInstanceOf(Carbon::class, $user->datetime_field);
}

public function testDatesFormattedAttributeBindings()
{
$bindings = [];

$this->app->make('db')->listen(static function ($query) use (&$bindings) {
$bindings = $query->bindings;
});

$user = TestModel1::create([
'date_field' => '2019-10',
'datetime_field' => '2019-10-01 10:15:20',
'immutable_date_field' => '2019-10-01',
'immutable_datetime_field' => '2019-10-01 10:15',
]);

$this->assertSame(['2019-10', '2019-10-01 10:15:20', '2019-10-01', '2019-10-01 10:15'], $bindings);
}

public function testDatesFormattedArrayAndJson()
{
$user = TestModel1::create([
'date_field' => '2019-10',
'datetime_field' => '2019-10-01 10:15:20',
'immutable_date_field' => '2019-10-01',
'immutable_datetime_field' => '2019-10-01 10:15',
]);

$expected = [
'date_field' => '2019-10',
'datetime_field' => '2019-10 10:15',
'immutable_date_field' => '2019-10',
'immutable_datetime_field' => '2019-10 10:15',
'id' => 1,
];

$this->assertSame($expected, $user->toArray());
$this->assertSame(json_encode($expected), $user->toJson());
}

public function testCustomDateCastsAreComparedAsDatesForCarbonInstances()
{
/** @var TestModel1 */
$user = TestModel1::create([
'date_field' => '2019-10-01',
'datetime_field' => '2019-10-01 10:15:20',
'immutable_date_field' => '2019-10-01',
'immutable_datetime_field' => '2019-10-01 10:15:20',
]);

$user->date_field = new Carbon('2019-10-01');
$user->datetime_field = new Carbon('2019-10-01 10:15:20');
$user->immutable_date_field = new CarbonImmutable('2019-10-01');
$user->immutable_datetime_field = new CarbonImmutable('2019-10-01 10:15:20');

$this->assertArrayNotHasKey('date_field', $user->getDirty());
$this->assertArrayNotHasKey('datetime_field', $user->getDirty());
$this->assertArrayNotHasKey('immutable_date_field', $user->getDirty());
$this->assertArrayNotHasKey('immutable_datetime_field', $user->getDirty());
}

public function testCustomDateCastsAreComparedAsDatesForStringValues()
{
/** @var TestModel1 */
$user = TestModel1::create([
'date_field' => '2019-10-01',
'datetime_field' => '2019-10-01 10:15:20',
'immutable_date_field' => '2019-10-01',
'immutable_datetime_field' => '2019-10-01 10:15:20',
]);

$user->date_field = '2019-10-01';
$user->datetime_field = '2019-10-01 10:15:20';
$user->immutable_date_field = '2019-10-01';
$user->immutable_datetime_field = '2019-10-01 10:15:20';

$this->assertArrayNotHasKey('date_field', $user->getDirty());
$this->assertArrayNotHasKey('datetime_field', $user->getDirty());
$this->assertArrayNotHasKey('immutable_date_field', $user->getDirty());
$this->assertArrayNotHasKey('immutable_datetime_field', $user->getDirty());
}
}

class TestModel1 extends Model
Expand All @@ -47,5 +131,7 @@ class TestModel1 extends Model
public $casts = [
'date_field' => 'date:Y-m',
'datetime_field' => 'datetime:Y-m H:i',
'immutable_date_field' => 'date:Y-m',
'immutable_datetime_field' => 'datetime:Y-m H:i',
];
}