BUGFIX: Disable DataObject validation temporarily while importing yaml fixtures

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@71642 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Andrew O'Neil 2009-02-11 00:18:20 +00:00 committed by Sam Minnee
parent bec773db22
commit 4cbf0d60ae
2 changed files with 37 additions and 6 deletions

View File

@ -105,6 +105,29 @@ class DataObject extends ViewableData implements DataObjectInterface,i18nEntityP
* @todo Define the options that can be set here
*/
static $api_access = false;
/**
* Should dataobjects be validated before they are written?
*/
private static $validation_enabled = true;
/**
* Returns when validation on DataObjects is enabled.
* @return bool
*/
static function get_validation_enabled() {
return self::$validation_enabled;
}
/**
* Set whether DataObjects should be validated before they are written.
* @param $enable bool
* @see DataObject::validate()
*/
static function set_validation_enabled($enable) {
self::$validation_enabled = (bool) $enable;
}
/**
* Construct a new DataObject.
@ -683,10 +706,12 @@ class DataObject extends ViewableData implements DataObjectInterface,i18nEntityP
$this->brokenOnWrite = true;
$isNewRecord = false;
$valid = $this->validate();
if(!$valid->valid()) {
throw new ValidationException($valid, "Validation error writing a $this->class object: " . $valid->message() . ". Object not written.", E_USER_WARNING);
return false;
if(self::get_validation_enabled()) {
$valid = $this->validate();
if(!$valid->valid()) {
throw new ValidationException($valid, "Validation error writing a $this->class object: " . $valid->message() . ". Object not written.", E_USER_WARNING);
return false;
}
}
$this->onBeforeWrite();

View File

@ -66,9 +66,15 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
$dbadmin = new DatabaseAdmin();
$dbadmin->clearAllData();
// We have to disable validation while we import the fixtures, as the order in
// which they are imported doesnt guarantee valid relations until after the
// import is complete.
$validationenabled = DataObject::get_validation_enabled();
DataObject::set_validation_enabled(false);
$this->fixture = new YamlFixture($fixtureFile);
$this->fixture->saveIntoDatabase();
DataObject::set_validation_enabled($validationenabled);
}
// Set up email
@ -230,4 +236,4 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
}
}
?>
?>