From 15fccb922b5cd4d54990ef685d39d079d72918d9 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Thu, 26 Nov 2026 21:28:34 +0000 Subject: [PATCH 1/2] Stop using deprecated `ClassMetadata::getReflectionProperty()` Doctrine ORM 3.8 deprecates direct access to `ClassMetadata::$reflFields`, which will be removed in ORM 4.0. `getReflectionProperty()` is itself deprecated for the same reason, since it still reads from that array internally, so every call to it triggered a deprecation warning. Replace those calls with `ReflectionService::getAccessibleProperty()`, the same non-deprecated pattern this class already uses in `findTranslatedProperties()`. A naive replacement using the current class name broke properties declared in mapped superclasses, since PHP's `ReflectionProperty` cannot resolve a private property by name when given a subclass. The declaring class is now read from the field/association mapping's `declared` entry, mirroring what Doctrine does internally. Doctrine ORM 2 represents mappings as plain arrays rather than `FieldMapping`/`AssociationMapping` objects, so the helper handles both representations to keep working across the `doctrine/orm: ^2.13 || ^3.0` range this bundle supports. --- src/Doctrine/TranslatableClassMetadata.php | 27 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Doctrine/TranslatableClassMetadata.php b/src/Doctrine/TranslatableClassMetadata.php index 6ec22db..267b1d3 100644 --- a/src/Doctrine/TranslatableClassMetadata.php +++ b/src/Doctrine/TranslatableClassMetadata.php @@ -12,6 +12,7 @@ use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadataFactory; +use Doctrine\Persistence\Mapping\ReflectionService; use Doctrine\Persistence\Mapping\RuntimeReflectionService; use InvalidArgumentException; use Psr\Log\LoggerInterface; @@ -229,6 +230,19 @@ private function findTranslatedProperties(ClassMetadata $cm, ClassMetadataFactor } } + /** + * Doctrine ORM 2 represents field/association mappings as plain arrays, while ORM 3 uses + * FieldMapping/AssociationMapping objects (which still support array access for BC, but that + * is deprecated for FieldMapping). The "declared" entry holds the class that originally + * declared the field, e.g. a mapped superclass, which can differ from the entity class. + */ + private static function getDeclaringClassName(array|object $mapping, string $fallbackClassName): string + { + $declaringClass = \is_array($mapping) ? ($mapping['declared'] ?? null) : $mapping->declared; + + return $declaringClass ?? $fallbackClassName; + } + private static function getAttributesIncludingParents(ReflectionClass $rc, ?string $attributeName = null, int $flags = 0): array { $attributes = []; @@ -245,6 +259,8 @@ private static function getAttributesIncludingParents(ReflectionClass $rc, ?stri private function findTranslationsCollection(ClassMetadata $cm, ClassMetadataFactory $classMetadataFactory): void { + $reflectionService = $classMetadataFactory->getReflectionService(); + foreach ($cm->associationMappings as $fieldName => $mapping) { if (isset($mapping['inherited'])) { // "inherited" means that there is another (inheritance parent) entity class containing this @@ -253,15 +269,16 @@ private function findTranslationsCollection(ClassMetadata $cm, ClassMetadataFact continue; } - $reflectionProperty = $cm->getReflectionProperty($fieldName); + $reflectionProperty = $reflectionService->getAccessibleProperty(self::getDeclaringClassName($mapping, $cm->name), $fieldName); if ($reflectionProperty->getAttributes(Attribute\TranslationCollection::class)) { $this->translationsCollectionProperty = $reflectionProperty; $translationEntityMetadata = $classMetadataFactory->getMetadataFor($mapping['targetEntity']); + $mappedByMapping = $translationEntityMetadata->associationMappings[$mapping['mappedBy']]; $this->translationClass = $translationEntityMetadata->getReflectionClass(); - $this->translationMappingProperty = $translationEntityMetadata->getReflectionProperty($mapping['mappedBy']); - $this->parseTranslationsEntity($translationEntityMetadata); + $this->translationMappingProperty = $reflectionService->getAccessibleProperty(self::getDeclaringClassName($mappedByMapping, $translationEntityMetadata->name), $mapping['mappedBy']); + $this->parseTranslationsEntity($translationEntityMetadata, $reflectionService); return; } @@ -288,10 +305,10 @@ private function isDeclaredByParentEntity(ReflectionProperty $property, ClassMet return $declaringClass !== $cm->name && $cm->parentClasses && is_a($cm->parentClasses[0], $declaringClass, true); } - private function parseTranslationsEntity(ClassMetadata $cm): void + private function parseTranslationsEntity(ClassMetadata $cm, ReflectionService $reflectionService): void { foreach ($cm->fieldMappings as $fieldName => $mapping) { - $reflectionProperty = $cm->getReflectionProperty($fieldName); + $reflectionProperty = $reflectionService->getAccessibleProperty(self::getDeclaringClassName($mapping, $cm->name), $fieldName); if ($reflectionProperty->getAttributes(Attribute\Locale::class)) { $this->translationLocaleProperty = $reflectionProperty; From 84aaedadb07d0cc29b91f2d1c1fe5dff771f47f8 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Thu, 26 Nov 2026 22:32:10 +0100 Subject: [PATCH 2/2] Update TranslatableClassMetadata.php --- src/Doctrine/TranslatableClassMetadata.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Doctrine/TranslatableClassMetadata.php b/src/Doctrine/TranslatableClassMetadata.php index 267b1d3..934c26c 100644 --- a/src/Doctrine/TranslatableClassMetadata.php +++ b/src/Doctrine/TranslatableClassMetadata.php @@ -230,14 +230,10 @@ private function findTranslatedProperties(ClassMetadata $cm, ClassMetadataFactor } } - /** - * Doctrine ORM 2 represents field/association mappings as plain arrays, while ORM 3 uses - * FieldMapping/AssociationMapping objects (which still support array access for BC, but that - * is deprecated for FieldMapping). The "declared" entry holds the class that originally - * declared the field, e.g. a mapped superclass, which can differ from the entity class. - */ private static function getDeclaringClassName(array|object $mapping, string $fallbackClassName): string { + // The "declared" entry holds the class that originally declared the field, + // e.g. a mapped superclass, which can differ from the entity class. $declaringClass = \is_array($mapping) ? ($mapping['declared'] ?? null) : $mapping->declared; return $declaringClass ?? $fallbackClassName;