BUGFIX Only loading value in a CompositeDBField through DataObject->getField() if either the value exists, or the record was previously saved

ENHANCEMENT Getting composite db field definitions in DataObject->hasOwnTableDatabaseField() when dealing with a CompositeDBField class

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@76097 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-05-05 07:55:46 +00:00
parent 3e18112b32
commit b4ec97506a

View File

@ -901,7 +901,6 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
} }
} }
} }
$this->extend('augmentWrite', $manipulation); $this->extend('augmentWrite', $manipulation);
// New records have their insert into the base data table done first, so that they can pass the // New records have their insert into the base data table done first, so that they can pass the
@ -938,7 +937,6 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
if($writeComponents) { if($writeComponents) {
$this->writeComponents(true); $this->writeComponents(true);
} }
return $this->record['ID']; return $this->record['ID'];
} }
@ -1810,7 +1808,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
public function getField($field) { public function getField($field) {
// If we already have an object in $this->record, then we should just return that // If we already have an object in $this->record, then we should just return that
if(isset($this->record[$field]) && is_object($this->record[$field])) return $this->record[$field]; if(isset($this->record[$field]) && is_object($this->record[$field])) return $this->record[$field];
// Otherwise, we need to determine if this is a complex field // Otherwise, we need to determine if this is a complex field
$fieldClass = $this->db($field); $fieldClass = $this->db($field);
if($fieldClass && ClassInfo::classImplements($fieldClass, 'CompositeDBField')) { if($fieldClass && ClassInfo::classImplements($fieldClass, 'CompositeDBField')) {
@ -1818,7 +1816,12 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$constructor = $helperPair['castingHelper']; $constructor = $helperPair['castingHelper'];
$fieldName = $field; $fieldName = $field;
$fieldObj = eval($constructor); $fieldObj = eval($constructor);
if(isset($this->record[$field])) $fieldObj->setValue($this->record[$field], $this->record);
// write value only if either the field value exists,
// or a valid record has been loaded from the database
$value = (isset($this->record[$field])) ? $this->record[$field] : null;
if($value || $this->exists()) $fieldObj->setValue($value, $this->record);
$this->record[$field] = $fieldObj; $this->record[$field] = $fieldObj;
return $this->record[$field]; return $this->record[$field];
@ -2000,6 +2003,14 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
if(!$fieldMap) { if(!$fieldMap) {
// all $db fields on this specific class (no parents) // all $db fields on this specific class (no parents)
$fieldMap = $this->uninherited('db', true); $fieldMap = $this->uninherited('db', true);
if($fieldMap) foreach($fieldMap as $fieldname => $fieldtype){
if(ClassInfo::classImplements($fieldtype, 'CompositeDBField')){
$combined_db = singleton($fieldtype)->composite_db();
foreach($combined_db as $name => $type){
$fieldMap[$fieldname.$name] = $type;
}
}
}
// all has_one relations on this specific class, // all has_one relations on this specific class,
// add foreign key // add foreign key
@ -2007,11 +2018,11 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
if($hasOne) foreach($hasOne as $fieldName => $fieldSchema) { if($hasOne) foreach($hasOne as $fieldName => $fieldSchema) {
$fieldMap[$fieldName . 'ID'] = "ForeignKey"; $fieldMap[$fieldName . 'ID'] = "ForeignKey";
} }
// set cached fieldmap // set cached fieldmap
self::$cache_has_own_table_field[$this->class] = $fieldMap; self::$cache_has_own_table_field[$this->class] = $fieldMap;
} }
// Remove string-based "constructor-arguments" from the DBField definition // Remove string-based "constructor-arguments" from the DBField definition
return isset($fieldMap[$field]) ? strtok($fieldMap[$field],'(') : null; return isset($fieldMap[$field]) ? strtok($fieldMap[$field],'(') : null;
} }