diff --git a/src/Doctrine/TranslatableClassMetadata.php b/src/Doctrine/TranslatableClassMetadata.php index 8ee6936..3e6b77c 100644 --- a/src/Doctrine/TranslatableClassMetadata.php +++ b/src/Doctrine/TranslatableClassMetadata.php @@ -232,8 +232,10 @@ private function findTranslatedProperties(ClassMetadata $cm, ClassMetadataFactor private function findTranslationsCollection(ClassMetadata $cm, ClassMetadataFactory $classMetadataFactory): void { foreach ($cm->associationMappings as $fieldName => $mapping) { - if (isset($mapping['declared'])) { - // The association is inherited from a parent class + if (isset($mapping['inherited'])) { + // "inherited" means that there is another (inheritance parent) entity class containing this + // field (https://github.com/doctrine/orm/blob/580a95ce3f5f016547d15ecc6cc94dd85453bed5/src/Mapping/AssociationMapping.php#L34-L46). + // Since PolyglotListener::getTranslationMetadatas() loops over these parent classes as well, we can skip the field here. continue; } diff --git a/tests/Fixtures/Entity/EntityInheritance/EntityInheritance_MappedSuperclassTranslation.php b/tests/Fixtures/Entity/EntityInheritance/EntityInheritance_MappedSuperclassTranslation.php new file mode 100644 index 0000000..f1b0068 --- /dev/null +++ b/tests/Fixtures/Entity/EntityInheritance/EntityInheritance_MappedSuperclassTranslation.php @@ -0,0 +1,26 @@ +translations = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function setText(TranslatableInterface $text): void + { + $this->text = $text; + } + + public function getText(): TranslatableInterface|string|null + { + return $this->text; + } +} diff --git a/tests/Fixtures/Entity/EntityInheritance/EntityInheritance_MappedSuperclassWithTranslationsInterface.php b/tests/Fixtures/Entity/EntityInheritance/EntityInheritance_MappedSuperclassWithTranslationsInterface.php new file mode 100644 index 0000000..b97506e --- /dev/null +++ b/tests/Fixtures/Entity/EntityInheritance/EntityInheritance_MappedSuperclassWithTranslationsInterface.php @@ -0,0 +1,7 @@ +addResolveTargetEntity( + EntityInheritance_MappedSuperclassWithTranslationsInterface::class, + EntityInheritance_MappedSuperclassWithTranslations_Entity::class, + [] + ); + + $this->entityManager->getEventManager()->addEventSubscriber($resolveTargetEntity); + + self::setupSchema([ + EntityInheritance_MappedSuperclassWithTranslations::class, + EntityInheritance_MappedSuperclassWithTranslations_Entity::class, + EntityInheritance_MappedSuperclassTranslation::class, + ]); + } + + public function testPersistAndReloadEntity(): void + { + $entity = new EntityInheritance_MappedSuperclassWithTranslations_Entity(); + $t = new Translatable('base text'); + $t->setTranslation('Basistext', 'de_DE'); + $entity->setText($t); + + self::import([$entity]); + + $loaded = $this->entityManager->find(EntityInheritance_MappedSuperclassWithTranslations_Entity::class, $entity->getId()); + + self::assertSame('base text', $loaded->getText()->translate('en_GB')); + self::assertSame('Basistext', $loaded->getText()->translate('de_DE')); + } + + public function testAddTranslation(): void + { + $entityManager = $this->entityManager; + + $entity = new EntityInheritance_MappedSuperclassWithTranslations_Entity(); + $entity->setText(new Translatable('base text')); + self::import([$entity]); + + $loaded = $entityManager->find(EntityInheritance_MappedSuperclassWithTranslations_Entity::class, $entity->getId()); + $loaded->getText()->setTranslation('Basistext', 'de_DE'); + $entityManager->flush(); + + $entityManager->clear(); + $reloaded = $entityManager->find(EntityInheritance_MappedSuperclassWithTranslations_Entity::class, $entity->getId()); + + self::assertSame('base text', $reloaded->getText()->translate('en_GB')); + self::assertSame('Basistext', $reloaded->getText()->translate('de_DE')); + } + + public function testUpdateTranslations(): void + { + $entityManager = $this->entityManager; + + $entity = new EntityInheritance_MappedSuperclassWithTranslations_Entity(); + $t = new Translatable('old text'); + $t->setTranslation('alter Text', 'de_DE'); + $entity->setText($t); + self::import([$entity]); + + $loaded = $entityManager->find(EntityInheritance_MappedSuperclassWithTranslations_Entity::class, $entity->getId()); + $loaded->getText()->setTranslation('new text'); + $loaded->getText()->setTranslation('neuer Text', 'de_DE'); + $entityManager->flush(); + + $entityManager->clear(); + $reloaded = $entityManager->find(EntityInheritance_MappedSuperclassWithTranslations_Entity::class, $entity->getId()); + + self::assertSame('new text', $reloaded->getText()->translate('en_GB')); + self::assertSame('neuer Text', $reloaded->getText()->translate('de_DE')); + } +}