diff --git a/forms/ComplexTableField.php b/forms/ComplexTableField.php index 435d970dd..931b57980 100755 --- a/forms/ComplexTableField.php +++ b/forms/ComplexTableField.php @@ -41,6 +41,12 @@ class ComplexTableField extends TableListField { */ protected $parentClass; + /** + * @var string Database column name for the used relation (e.g. FamilyID + * if one Family has_many Individuals). + */ + protected $parentIdName; + /** * @var array Influence output without having to subclass the template. */ @@ -250,6 +256,7 @@ JS; } else { $detailFields = $childData->getCMSFields(); } + if($this->getParentClass()) { $parentIdName = $this->getParentIdName($this->sourceClass,$this->getParentClass()); if(!$parentIdName) { @@ -261,6 +268,7 @@ JS; } // add relational fields $detailFields->push(new HiddenField("ctf[parentClass]"," ",$this->getParentClass())); + if( $this->relationAutoSetting ) $detailFields->push(new HiddenField("$parentIdName"," ",$ID)); } @@ -570,10 +578,22 @@ JS; return $this->getParentIdNameRelation( $childClass, $parentClass, 'has_one' ); } + /** + * Manually overwrites the parent-ID relations. + * @see setParentClass() + * + * @param String $str Example: FamilyID (when one Individual has_one Family) + */ + function setParentIdName($str) { + $this->parentIdName = $str; + } + /** * Returns the db-fieldname of the currently used relationship. */ function getParentIdNameRelation( $parentClass, $childClass, $relation ){ + if($this->parentIdName) return $this->parentIdName; + $relations = singleton( $parentClass )->$relation(); $classes = ClassInfo::ancestry( $childClass ); foreach( $relations as $k => $v ) { @@ -700,7 +720,7 @@ class ComplexTableField_Popup extends Form { $childObject = new $this->sourceClass(); $this->fields->removeByName('ID'); } - + $this->saveInto($childObject); $childObject->write();