From 3f1805bfd2934bdf2a489fdd00b5c21308cf9837 Mon Sep 17 00:00:00 2001 From: Josh Date: Fri, 23 Jan 2015 23:11:08 +0000 Subject: [PATCH] NEW: Support multiple many_manys between the same classes (closes #1377) --- model/DataObject.php | 45 +++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/model/DataObject.php b/model/DataObject.php index 37d114ffd..d294a184c 100644 --- a/model/DataObject.php +++ b/model/DataObject.php @@ -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);