-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This affects model attribute casts 'date', 'datetime', 'immutable_date', and 'immutable_datetime' with a custom date format being used. e.g., $casts = ['month' => 'datetime:Y-m'] Last week a fix was introduced for attributes being set with Carbon objects that were mistakenly flagged as "dirty" in the model even though dates were the same. However that fix broke behavior of date _strings_ such as $request->validated() being filled into a model. For: $model->fill(['month' => '2021-09'])->save(); Instead of the SQL query binding being "2021-09", it's "2021-09-01 00:00:00" because the date string is reformatted through fromDateTime(), using getDateFormat() and not the custom cast date. Keep the v8.60.0 behavior that sets the raw date string in the attributes payload which is eventually bound in the database query. Avoid going through fromDateTime() that reformats that string. Also remove the added redundant conditional in originalIsEquivalent() as method isDateAttribute() already includes isDateCastable().
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,46 @@ public function testDatesAreCustomCastable() | |
$this->assertInstanceOf(Carbon::class, $user->datetime_field); | ||
} | ||
|
||
public function testCustomDateCastsAreComparedAsDates() | ||
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' => new CarbonImmutable('2019-10-01 10:15:20'), | ||
'immutable_date_field' => new CarbonImmutable('2019-10-01'), | ||
'immutable_datetime_field' => '2019-10-01 10:15', | ||
]); | ||
|
||
$this->assertSame(['2019-10', '2019-10-01 10:15:20', '2019-10-01 00:00:00', '2019-10-01 10:15'], $bindings); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
derekmd
Author
Contributor
|
||
} | ||
|
||
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()); | ||
} | ||
|
||
public function testCustomDateCastsAreComparedAsDatesForCarbonValues() | ||
{ | ||
/** @var TestModel1 */ | ||
$user = TestModel1::create([ | ||
|
@derekmd I’m working on a fix for #38828 and #38720.
I had to update your test for it to pass against the current 8.x branch: 04ae160
Was there a reason you expected the
Y-m
andY-m-d H:i
formats in the $bindings, rather thanY-m-d
andY-m-d H:i:s
?