BUGFIX #5316 Float and Double should never try to save NULL as the "null" value

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@102460 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2010-04-12 06:02:22 +00:00 committed by Sam Minnee
parent 41fec96e5e
commit 53f2f7a873
3 changed files with 70 additions and 11 deletions

View File

@ -20,5 +20,32 @@ class Double extends DBField {
function Nice() { function Nice() {
return number_format($this->value, 2); return number_format($this->value, 2);
} }
/**
* Returns the value to be set in the database to blank this field.
* Usually it's a choice between null, 0, and ''
*/
function nullValue() {
return 0;
}
/**
* Return an encoding of the given value suitable for inclusion in a SQL statement.
* If necessary, this should include quotes.
*/
function prepValueForDB($value) {
if($value === true) {
return 1;
}
if(!$value || !is_numeric($value)) {
if(strpos($value, '[') === false) {
return '0';
} else {
return Convert::raw2sql($value);
}
} else {
return Convert::raw2sql($value);
}
}
} }
?>

View File

@ -34,6 +34,14 @@ class Float extends DBField {
return new NumericField($this->name, $title); return new NumericField($this->name, $title);
} }
/**
* Returns the value to be set in the database to blank this field.
* Usually it's a choice between null, 0, and ''
*/
function nullValue() {
return 0;
}
/** /**
* Return an encoding of the given value suitable for inclusion in a SQL statement. * Return an encoding of the given value suitable for inclusion in a SQL statement.
* If necessary, this should include quotes. * If necessary, this should include quotes.
@ -41,15 +49,16 @@ class Float extends DBField {
function prepValueForDB($value) { function prepValueForDB($value) {
if($value === true) { if($value === true) {
return 1; return 1;
} if(!$value || !is_numeric($value)) { }
if(strpos($value, '[')===false) if(!$value || !is_numeric($value)) {
if(strpos($value, '[') === false) {
return '0'; return '0';
else
return addslashes($value);
} else { } else {
return addslashes($value); return Convert::raw2sql($value);
}
} else {
return Convert::raw2sql($value);
} }
} }
} }
?>

View File

@ -9,11 +9,34 @@
*/ */
class DBFieldTest extends SapphireTest { class DBFieldTest extends SapphireTest {
/**
* Test the nullValue() method on DBField.
*/
function testNullValue() {
/* Float and Double use 0 for "null" value representation */
$this->assertEquals(0, singleton('Float')->nullValue());
$this->assertEquals(0, singleton('Double')->nullValue());
}
/** /**
* Test the prepValueForDB() method on DBField. * Test the prepValueForDB() method on DBField.
*/ */
function testPrepValueForDB() { function testPrepValueForDB() {
/* Float behaviour, asserting we have 0 */
$this->assertEquals('0', singleton('Float')->prepValueForDB(0));
$this->assertEquals('0', singleton('Float')->prepValueForDB(null));
$this->assertEquals('0', singleton('Float')->prepValueForDB(false));
$this->assertEquals('0', singleton('Float')->prepValueForDB(''));
$this->assertEquals('0', singleton('Float')->prepValueForDB('0'));
/* Double behaviour, asserting we have 0 */
$this->assertEquals('0', singleton('Double')->prepValueForDB(0));
$this->assertEquals('0', singleton('Double')->prepValueForDB(null));
$this->assertEquals('0', singleton('Double')->prepValueForDB(false));
$this->assertEquals('0', singleton('Double')->prepValueForDB(''));
$this->assertEquals('0', singleton('Double')->prepValueForDB('0'));
/* Integer behaviour, asserting we have 0 */ /* Integer behaviour, asserting we have 0 */
$this->assertEquals('0', singleton('Int')->prepValueForDB(0)); $this->assertEquals('0', singleton('Int')->prepValueForDB(0));
$this->assertEquals('0', singleton('Int')->prepValueForDB(null)); $this->assertEquals('0', singleton('Int')->prepValueForDB(null));