diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 8a85b19853c..5e65c05daa3 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -381,10 +381,6 @@ private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $p if ($mapping['type'] & ClassMetadata::TO_MANY && ! $mapping['isOwningSide']) { throw MappingException::illegalToManyAssociationOnMappedSuperclass($parentClass->name, $field); } - - if (! isset($mapping['inherited'])) { - $mapping['sourceEntity'] = $subClass->name; - } } //$subclassMapping = $mapping; @@ -396,6 +392,17 @@ private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $p $mapping['declared'] = $parentClass->name; } + // When the class inheriting the relation ($subClass) is the first entity class since the + // relation has been defined in a mapped superclass (or in a chain + // of mapped superclasses) above, then declare this current entity class as the source of + // the relationship. + // According to the definitions given in https://github.com/doctrine/orm/pull/10396/, + // this is the case when isset($mapping['declared']) && ! isset($mapping['inherited']) + // <=> the relationship is declared elsewhere but not yet part of any other _entity_ class. + if (isset($mapping['declared']) && ! isset($mapping['inherited'])) { + $mapping['sourceEntity'] = $subClass->name; + } + $subClass->addInheritedAssociationMapping($mapping); } }