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
This commit is contained in:
Sean Harvey 2012-02-24 16:18:36 +13:00 committed by Sean Harvey
parent dc439ed0e2
commit 8fdc531345
4 changed files with 67 additions and 10 deletions

View File

@ -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 @@ class Date extends DBField {
public function scaffoldFormField($title = null, $params = null) {
return new DateField($this->name, $title);
}
}
}

View File

@ -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 @@ class SS_Datetime extends Date {
* 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 @@ class SS_Datetime extends Date {
}
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 File

@ -93,5 +93,25 @@ class DateTest extends SapphireTest {
"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 File

@ -33,4 +33,22 @@ class SS_DatetimeTest extends SapphireTest {
$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');
}
}