Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/Doctrine/TranslatableClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -229,6 +230,15 @@ private function findTranslatedProperties(ClassMetadata $cm, ClassMetadataFactor
}
}

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;
}

private static function getAttributesIncludingParents(ReflectionClass $rc, ?string $attributeName = null, int $flags = 0): array
{
$attributes = [];
Expand All @@ -245,6 +255,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
Expand All @@ -253,15 +265,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;
}
Expand All @@ -288,10 +301,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;
Expand Down
Loading