Skip to content

Commit a09f82d

Browse files
committed
* Fix bug when setting the main date field to null.
1 parent 658d365 commit a09f82d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/HasSplittedDates.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ protected function castSplittedDate(string $attribute, $value)
161161
if ($attribute === $key) {
162162
parent::setAttribute($key, $value);
163163
foreach ($map as $mapKey) {
164-
$this->attributes["{$key}_{$mapKey}"] = $this->$attribute->$mapKey;
164+
$this->attributes["{$key}_{$mapKey}"] = $this->$attribute ? $this->$attribute->$mapKey : null;
165165
}
166166
} else {
167167
$this->attributes[$attribute] = $value;

tests/ModelSplittedDateTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,29 @@ public function testHourOverflow()
295295
$this->assertEquals(27, $model->end_at_hour);
296296
}
297297

298+
/**
299+
* @group CommonTests
300+
* @group CommonUnitTests
301+
* @group ModelSplittedDatesTest
302+
*/
303+
public function testDateSetToNullSetsAllSplittedValuesToNull()
304+
{
305+
$model = $this->createAnonymousModel([
306+
'end_at'
307+
, 'end_at_year'
308+
, 'end_at_month'
309+
, 'end_at_day'
310+
], [
311+
'end_at'
312+
]);
313+
314+
$model->end_at = '2027-07-15 19:56:01';
315+
$this->assertEquals(15, $model->end_at_day);
316+
317+
$model->end_at = null;
318+
$this->assertNull($model->end_at_day);
319+
$this->assertNull($model->end_at_month);
320+
$this->assertNull($model->end_at_year);
321+
}
322+
298323
}

0 commit comments

Comments
 (0)