BUGFIX #5009: Removed inappropriate field-detection change on multienums with no default

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@97980 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-02-02 02:51:48 +00:00
parent 11368564a3
commit 2294d0986b
2 changed files with 60 additions and 7 deletions

View File

@ -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;
}
}
?>
?>

View File

@ -0,0 +1,40 @@
<?php
class DataObjectSchemaGenerationTest extends SapphireTest {
protected $extraDataObjects = array(
'DataObjectSchemaGenerationTest_DO',
);
/**
* Check that once a schema has been generated, then it doesn't need any more updating
*/
function testFieldsDontRerequestChanges() {
$db = DB::getConn();
DB::quiet();
// Table will have been initially created by the $extraDataObjects setting
// Verify that it doesn't need to be recreated
$db->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")',
);
}