From 1997485ba3108ccd3b916b3eebb763664461278e Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 13 Apr 2010 01:46:27 +0000 Subject: [PATCH] BUGFIX #5009: Removed inappropriate field-detection change on multienums with no default (from r97980) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102561 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/fieldtypes/MultiEnum.php | 27 +++++++++---- .../model/DataObjectSchemaGenerationTest.php | 40 +++++++++++++++++++ 2 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 tests/model/DataObjectSchemaGenerationTest.php diff --git a/core/model/fieldtypes/MultiEnum.php b/core/model/fieldtypes/MultiEnum.php index e0895f86c..86bbe1aad 100755 --- a/core/model/fieldtypes/MultiEnum.php +++ b/core/model/fieldtypes/MultiEnum.php @@ -12,14 +12,27 @@ */ class MultiEnum extends Enum { function __construct($name, $enum = NULL, $default = NULL) { - parent::__construct($name, $enum, $default); - - // MultiEnums are allowed blank defaults - if(!$default) $this->default = $default; + // MultiEnum needs to take care of its own defaults + parent::__construct($name, $enum, null); + + // Validate and assign the default + $this->default = null; + if($default) { + $defaults = preg_split('/ *, */',trim($default)); + foreach($defaults as $thisDefault) { + if(!in_array($thisDefault, $this->enum)) { + user_error("Enum::__construct() The default value '$thisDefault' does not match " + . "any item in the enumeration", E_USER_ERROR); + return; + } + } + $this->default = implode(',',$defaults); + } } function requireField(){ - DB::requireField($this->tableName, $this->name, "set('" . implode("','", $this->enum) . "') character set utf8 collate utf8_general_ci default '{$this->default}'"); + $defaultClause = $this->default ? " default '". Convert::raw2sql($this->default) . "'" : ""; + DB::requireField($this->tableName, $this->name, "set('" . implode("','", $this->enum) . "') character set utf8 collate utf8_general_ci$defaultClause"); } @@ -32,8 +45,8 @@ class MultiEnum extends Enum { $field = new CheckboxSetField($name, $title, $this->enumValues($hasEmpty), $value, $form); - return $field; + return $field; } } -?> \ No newline at end of file + ?> \ No newline at end of file diff --git a/tests/model/DataObjectSchemaGenerationTest.php b/tests/model/DataObjectSchemaGenerationTest.php new file mode 100644 index 000000000..97a990fbc --- /dev/null +++ b/tests/model/DataObjectSchemaGenerationTest.php @@ -0,0 +1,40 @@ +beginSchemaUpdate(); + $obj = new DataObjectSchemaGenerationTest_DO(); + $obj->requireTable(); + $needsUpdating = $db->doesSchemaNeedUpdating(); + $db->cancelSchemaUpdate(); + + $this->assertFalse($needsUpdating); + } +} + +class DataObjectSchemaGenerationTest_DO extends DataObject implements TestOnly { + static $db = array( + 'MultiEnum1' => 'MultiEnum("A, B, C, D","")', + 'MultiEnum2' => 'MultiEnum("A, B, C, D","A")', + 'MultiEnum3' => 'MultiEnum("A, B, C, D","A, B")', + + 'Enum1' => 'Enum("A, B, C, D","")', + 'Enum2' => 'Enum("A, B, C, D","A")', + ); + +} \ No newline at end of file