From 0aa13da0d99a94a0a6dc4cc8cf42e37abcf5374a Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Wed, 6 Jun 2018 09:43:04 +1200 Subject: [PATCH] BUG Backport bugfix for belongs_many_many with many_many through. Partial backport of #7928 Fixes #8131 --- src/ORM/DataObjectSchema.php | 14 +++++++++++--- tests/php/ORM/ManyManyThroughListTest/Item.php | 3 ++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/ORM/DataObjectSchema.php b/src/ORM/DataObjectSchema.php index e309a1660..8251c8477 100644 --- a/src/ORM/DataObjectSchema.php +++ b/src/ORM/DataObjectSchema.php @@ -982,9 +982,17 @@ class DataObjectSchema if (!$otherManyMany) { return null; } - foreach ($otherManyMany as $inverseComponentName => $nextClass) { - if ($nextClass === $parentClass) { - return $inverseComponentName; + foreach ($otherManyMany as $inverseComponentName => $manyManySpec) { + // Normal many-many + if ($manyManySpec === $parentClass) { + return $inverseComponentName; + } + // many-many through, inspect 'to' for the many_many + if (is_array($manyManySpec)) { + $toClass = $this->hasOneComponent($manyManySpec['through'], $manyManySpec['to']); + if ($toClass === $parentClass) { + return $inverseComponentName; + } } } return null; diff --git a/tests/php/ORM/ManyManyThroughListTest/Item.php b/tests/php/ORM/ManyManyThroughListTest/Item.php index e717b3c84..9f96e7d14 100644 --- a/tests/php/ORM/ManyManyThroughListTest/Item.php +++ b/tests/php/ORM/ManyManyThroughListTest/Item.php @@ -19,6 +19,7 @@ class Item extends DataObject implements TestOnly ]; private static $belongs_many_many = [ - 'Objects' => 'SilverStripe\\ORM\\Tests\\ManyManyThroughListTest\\TestObject.Items' + // Intentionally omit parent `.Items` specifier to ensure it's not mandatory + 'Objects' => TestObject::class, ]; }