Skip to content

Commit

Permalink
Fix empty date/times being marshalled incorrectly.
Browse files Browse the repository at this point in the history
Empty values should be marshalled into null. Not doing this makes saving
empty date inputs impossible.

Refs #5723
  • Loading branch information
markstory committed Feb 3, 2015
1 parent f3642fb commit 4c9d704
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Database/Type/DateTimeType.php
Expand Up @@ -135,6 +135,9 @@ public function marshal($value)
return $value;
}

if (is_array($value) && implode('', $value) === '') {
return null;
}
$value += ['hour' => 0, 'minute' => 0, 'second' => 0];

$format = '';
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase/Database/Type/DateTimeTypeTest.php
Expand Up @@ -115,6 +115,10 @@ public function marshalProvider()
['2014-02-14 13:14:15', new Time('2014-02-14 13:14:15')],

// valid array types
[
['year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => ''],
null
],
[
['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15],
new Time('2014-02-14 13:14:15')
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase/Database/Type/DateTypeTest.php
Expand Up @@ -111,6 +111,10 @@ public function marshalProvider()
['2014-02-14', new Time('2014-02-14')],

// valid array types
[
['year' => '', 'month' => '', 'day' => ''],
null,
],
[
['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15],
new Time('2014-02-14 00:00:00')
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase/Database/Type/TimeTypeTest.php
Expand Up @@ -110,6 +110,14 @@ public function marshalProvider()
['13:10:10', new Time('13:10:10')],

// valid array types
[
['hour' => '', 'minute' => '', 'second' => ''],
null,
],
[
['hour' => '', 'minute' => '', 'meridian' => ''],
null,
],
[
['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15],
new Time('2014-02-14 13:14:15')
Expand Down

4 comments on commit 4c9d704

@renardrouge
Copy link

Choose a reason for hiding this comment

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

Excellent ! Thank you Mark. That fixed the problem.

Could it be a future requirement to automatically include "array('empty' => true, 'default' => '') in the generated "cake bake" code for date_of_birth in add.ctp and edit.ctp when defined as "date DEFAULT NULL" in the database ?

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

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

Sure you can make a pull request to cakephp/bake 😄

@dereuromark
Copy link
Member

Choose a reason for hiding this comment

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

@renardrouge Why default ''? The empty true part I understand - and agree ;)

@renardrouge
Copy link

Choose a reason for hiding this comment

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

Hello euro Mark !
With just 'empty' => true, this is the result:
null_date_8
and on Submit, the DEFAULT NULL field is filled with the current date.
If 'default' => '' is added, this is the result:
null_date_9
and on Submit, the DEFAULT NULL field is NULL.

Please sign in to comment.