From 9b6953d65b1eda59a1a73214913800321f1b93c5 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Wed, 8 Dec 2021 20:11:58 +0100 Subject: [PATCH] Only call refresh when the locale is different in the entity --- .../Gedmo/TranslatableAdminExtension.php | 10 ++++- .../Gedmo/TranslatableAdminExtensionTest.php | 43 ++++++++++++++++++- tests/Fixtures/Model/ModelTranslatable.php | 7 +++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/Admin/Extension/Gedmo/TranslatableAdminExtension.php b/src/Admin/Extension/Gedmo/TranslatableAdminExtension.php index ad332e44..737bb293 100644 --- a/src/Admin/Extension/Gedmo/TranslatableAdminExtension.php +++ b/src/Admin/Extension/Gedmo/TranslatableAdminExtension.php @@ -136,7 +136,6 @@ public function alterObject(AdminInterface $admin, $object) return; } - $objectManager->refresh($object); $this->setLocale($object, $admin); } @@ -214,6 +213,15 @@ private function setLocale(object $object, AdminInterface $admin): void $reflectionProperty = $reflClass->getProperty($configuration['locale']); $reflectionProperty->setAccessible(true); + + if ($reflectionProperty->getValue($object) === $translatableLocale) { + return; + } + $reflectionProperty->setValue($object, $translatableLocale); + + if ($objectManager->contains($object)) { + $objectManager->refresh($object); + } } } diff --git a/tests/Admin/Extension/Gedmo/TranslatableAdminExtensionTest.php b/tests/Admin/Extension/Gedmo/TranslatableAdminExtensionTest.php index 0e294b1d..9c83e1ee 100644 --- a/tests/Admin/Extension/Gedmo/TranslatableAdminExtensionTest.php +++ b/tests/Admin/Extension/Gedmo/TranslatableAdminExtensionTest.php @@ -93,7 +93,6 @@ public function get(): string public function testSetLocaleForTranslatableObject(): void { $object = new ModelTranslatable(); - $this->em->persist($object); // @phpstan-ignore-next-line Each extension will handle specific type $this->extension->alterNewInstance($this->admin, $object); @@ -126,6 +125,48 @@ public function testConfigureQuery(): void static::assertFalse($this->translatableListener->getTranslationFallback()); } + public function testRefreshIsCalledWithDifferentLocale(): void + { + $object = new ModelTranslatable(); + $object->locale = 'en'; + $this->em->persist($object); + $this->em->flush(); + + $object->refreshableField = 'new value'; + + /** + * NEXT_MAJOR: Remove this comment, each extension will handle specific type. + * + * @psalm-suppress InvalidArgument + * @phpstan-ignore-next-line + */ + $this->extension->alterObject($this->admin, $object); + + /** @psalm-suppress TypeDoesNotContainType */ + static::assertSame('', $object->refreshableField); + } + + public function testRefreshIsNotCalledWithTheSameLocale(): void + { + $object = new ModelTranslatable(); + $object->locale = 'es'; + $this->em->persist($object); + $this->em->flush(); + + $object->refreshableField = 'new value'; + + /** + * NEXT_MAJOR: Remove this comment, each extension will handle specific type. + * + * @psalm-suppress InvalidArgument + * @phpstan-ignore-next-line + */ + $this->extension->alterObject($this->admin, $object); + + /** @psalm-suppress RedundantCondition */ + static::assertSame('new value', $object->refreshableField); + } + protected function getUsedEntityFixtures(): array { return [ModelTranslatable::class]; diff --git a/tests/Fixtures/Model/ModelTranslatable.php b/tests/Fixtures/Model/ModelTranslatable.php index 478a3df4..339fc76e 100644 --- a/tests/Fixtures/Model/ModelTranslatable.php +++ b/tests/Fixtures/Model/ModelTranslatable.php @@ -35,4 +35,11 @@ class ModelTranslatable implements Translatable * @var string|null */ public $locale = null; + + /** + * @ORM\Column(type="string", length=10) + * + * @var string + */ + public $refreshableField = ''; }