Permalink
Browse files

BUGFIX #5316 Float and Double should never try to save NULL as the "n…

…ull" value (from r102460)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112067 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
sminnee committed Oct 13, 2010
1 parent 19f4c1e commit 268800dec614d78f5a5b0d34e503c4e353bbe5ef
Showing with 70 additions and 11 deletions.
  1. +30 −3 core/model/fieldtypes/Double.php
  2. +17 −8 core/model/fieldtypes/Float.php
  3. +23 −0 tests/fieldtypes/DBFieldTest.php
@@ -19,6 +19,33 @@ function requireField() {
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);
+ }
+ }
+
+}
@@ -34,22 +34,31 @@ public function scaffoldFormField($title = null) {
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.
*/
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);
}
}
-
-}
-?>
+
+}
@@ -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));

0 comments on commit 268800d

Please sign in to comment.