Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

BUGFIX Ensure Date and Datetime field types actually set NULL, false,…

… empty string values correctly instead of "1970-01-01" which gets saved to the database instead of NULL.

BUGFIX Datetime::Nice() and casting methods return NULL when there is no value, to be consistent with Date::Nice() and so on
  • Loading branch information...
commit 8fdc531345d96127f66cc5064dbb209e01cbdd87 1 parent dc439ed
@halkyon halkyon authored halkyon committed
View
13 model/fieldtypes/Date.php
@@ -21,17 +21,26 @@
class Date extends DBField {
function setValue($value) {
+ if($value === false || $value === null || (is_string($value) && !strlen($value))) {
+ // don't try to evaluate empty values with strtotime() below, as it returns "1970-01-01" when it should be saved as NULL in database
+ $this->value = null;
+ return;
+ }
+
// @todo This needs tidy up (what if you only specify a month and a year, for example?)
if(is_array($value)) {
if(!empty($value['Day']) && !empty($value['Month']) && !empty($value['Year'])) {
$this->value = $value['Year'] . '-' . $value['Month'] . '-' . $value['Day'];
return;
+ } else {
+ // return nothing (so ereg() doesn't fail on an empty array below)
+ return null;
}
}
// Default to NZ date format - strtotime expects a US date
if(ereg('^([0-9]+)/([0-9]+)/([0-9]+)$', $value, $parts)) {
- $value = "$parts[2]/$parts[1]/$parts[3]";
+ $value = "$parts[2]/$parts[1]/$parts[3]";
}
if(is_numeric($value)) {
@@ -357,4 +366,4 @@ static function past_date($fmonth, $fday = 1, $fyear = null) {
public function scaffoldFormField($title = null, $params = null) {
return new DateField($this->name, $title);
}
-}
+}
View
22 model/fieldtypes/Datetime.php
@@ -26,6 +26,12 @@
class SS_Datetime extends Date {
function setValue($value) {
+ if($value === false || $value === null || (is_string($value) && !strlen($value))) {
+ // don't try to evaluate empty values with strtotime() below, as it returns "1970-01-01" when it should be saved as NULL in database
+ $this->value = null;
+ return;
+ }
+
// Default to NZ date format - strtotime expects a US date
if(ereg('^([0-9]+)/([0-9]+)/([0-9]+)$', $value, $parts)) {
$value = "$parts[2]/$parts[1]/$parts[3]";
@@ -42,19 +48,23 @@ function setValue($value) {
* Returns the date in the raw SQL-format, e.g. “2006-01-18 16:32:04”
*/
function Nice() {
- return date('d/m/Y g:ia', strtotime($this->value));
+ if($this->value) return date('d/m/Y g:ia', strtotime($this->value));
}
+
function Nice24() {
- return date('d/m/Y H:i', strtotime($this->value));
+ if($this->value) return date('d/m/Y H:i', strtotime($this->value));
}
+
function Date() {
- return date('d/m/Y', strtotime($this->value));
+ if($this->value) return date('d/m/Y', strtotime($this->value));
}
+
function Time() {
- return date('g:ia', strtotime($this->value));
+ if($this->value) return date('g:ia', strtotime($this->value));
}
+
function Time24() {
- return date('H:i', strtotime($this->value));
+ if($this->value) return date('H:i', strtotime($this->value));
}
function requireField() {
@@ -64,7 +74,7 @@ function requireField() {
}
function URLDatetime() {
- return date('Y-m-d%20H:i:s', strtotime($this->value));
+ if($this->value) return date('Y-m-d%20H:i:s', strtotime($this->value));
}
public function scaffoldFormField($title = null, $params = null) {
View
22 tests/model/DateTest.php
@@ -93,5 +93,25 @@ function testLongDate() {
"Date->Long() works with D/M/YYYY"
);
}
-
+
+ function testSetNullAndZeroValues() {
+ $date = DBField::create('Date', '');
+ $this->assertNull($date->getValue(), 'Empty string evaluates to NULL');
+
+ $date = DBField::create('Date', null);
+ $this->assertNull($date->getValue(), 'NULL is set as NULL');
+
+ $date = DBField::create('Date', false);
+ $this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL');
+
+ $date = DBField::create('Date', array());
+ $this->assertNull($date->getValue(), 'Empty array evaluates to NULL');
+
+ $date = DBField::create('Date', '0');
+ $this->assertEquals('1970-01-01', $date->getValue(), 'Zero is UNIX epoch date');
+
+ $date = DBField::create('Date', 0);
+ $this->assertEquals('1970-01-01', $date->getValue(), 'Zero is UNIX epoch date');
+ }
+
}
View
20 tests/model/DatetimeTest.php
@@ -33,4 +33,22 @@ function testNowWithMockDate() {
$nowDatetime = SS_Datetime::now();
$this->assertEquals($systemDatetime->Date(), $nowDatetime->Date());
}
-}
+
+ function testSetNullAndZeroValues() {
+ $date = DBField::create('SS_Datetime', '');
+ $this->assertNull($date->getValue(), 'Empty string evaluates to NULL');
+
+ $date = DBField::create('SS_Datetime', null);
+ $this->assertNull($date->getValue(), 'NULL is set as NULL');
+
+ $date = DBField::create('SS_Datetime', false);
+ $this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL');
+
+ $date = DBField::create('SS_Datetime', '0');
+ $this->assertEquals('1970-01-01 12:00:00', $date->getValue(), 'Zero is UNIX epoch time');
+
+ $date = DBField::create('SS_Datetime', 0);
+ $this->assertEquals('1970-01-01 12:00:00', $date->getValue(), 'Zero is UNIX epoch time');
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.