NEW: Support multiple many_manys between the same classes (closes #1377)

This commit is contained in:
Josh 2015-01-23 23:11:08 +00:00 committed by Loz Calver
parent 319b96b48b
commit 3f1805bfd2

View File

@ -1931,17 +1931,29 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$manyMany = $SNG_class->stat('belongs_many_many');
$candidate = (isset($manyMany[$component])) ? $manyMany[$component] : null;
if($candidate) {
list($candidate,$varName) = explode('.',$candidate);
$SNG_candidate = singleton($candidate);
$candidateManyMany = $SNG_candidate->stat('many_many');
// Find the relation given the class
if($candidateManyMany) foreach($candidateManyMany as $relation => $relatedClass) {
if($relatedClass == $class) {
$relationName = $relation;
}
if( empty($varName) ) {
// Find the relation given the class
if($candidateManyMany) {
foreach($candidateManyMany as $relation => $relatedClass) {
if($relatedClass == $class) {
$relationName = $relation;
}
}
}
} else {
$relationName = $varName;
}
$extraFields = $SNG_candidate->stat('many_many_extraFields');
if( !isset($relationName) ) {
user_error("Inverse component of $candidate not found ({$this->class})", E_USER_ERROR);
}
$extraFields = $SNG_candidate->stat('many_many_extraFields');
if(isset($extraFields[$relationName])) {
return $extraFields[$relationName];
}
@ -1999,6 +2011,9 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$belongsManyMany = Config::inst()->get($class, 'belongs_many_many', Config::UNINHERITED);
$candidate = (isset($belongsManyMany[$component])) ? $belongsManyMany[$component] : null;
if($candidate) {
list($candidate,$varName) = explode('.',$candidate);
$childField = $candidate . "ID";
// We need to find the inverse component name
@ -2007,12 +2022,20 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
user_error("Inverse component of $candidate not found ({$this->class})", E_USER_ERROR);
}
foreach($otherManyMany as $inverseComponentName => $candidateClass) {
if($candidateClass == $class || is_subclass_of($class, $candidateClass)) {
$parentField = ($class == $candidate) ? "ChildID" : $candidateClass . "ID";
if( empty($varName) ) {
foreach($otherManyMany as $inverseComponentName => $candidateClass) {
if($candidateClass == $class || is_subclass_of($class, $candidateClass)) {
$parentField = ($class == $candidate) ? "ChildID" : $candidateClass . "ID";
return array($class, $candidate, $parentField, $childField,
"{$candidate}_$inverseComponentName");
return array($class, $candidate, $parentField,
$childField,"{$candidate}_$inverseComponentName");
}
}
} else {
if( isset($otherManyMany[$varName]) ) {
$candidateClass = $otherManyMany[$varName];
$parentField = ($class == $candidate) ? "ChildID" : $candidateClass . "ID";
return array($class, $candidate, $parentField, $childField,"{$candidate}_$varName");
}
}
user_error("Orphaned \$belongs_many_many value for $this->class.$component", E_USER_ERROR);