mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
NEW: Support multiple many_manys between the same classes (closes #1377)
This commit is contained in:
parent
319b96b48b
commit
3f1805bfd2
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user