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

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112067 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-10-13 01:43:58 +00:00
parent 19f4c1ef89
commit 268800dec6
3 changed files with 70 additions and 11 deletions

View File

@ -19,6 +19,33 @@ class Double extends DBField {
function Nice() {
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);
}
/**
* 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.
@ -41,15 +49,16 @@ class Float extends DBField {
function prepValueForDB($value) {
if($value === true) {
return 1;
} if(!$value || !is_numeric($value)) {
if(strpos($value, '[')===false)
}
if(!$value || !is_numeric($value)) {
if(strpos($value, '[') === false) {
return '0';
else
return addslashes($value);
} else {
return Convert::raw2sql($value);
}
} else {
return addslashes($value);
return Convert::raw2sql($value);
}
}
}
?>
}

View File

@ -9,11 +9,34 @@
*/
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.
*/
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 */
$this->assertEquals('0', singleton('Int')->prepValueForDB(0));
$this->assertEquals('0', singleton('Int')->prepValueForDB(null));