Skip to content

Commit

Permalink
Only call refresh when the locale is different in the entity
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored and jordisala1991 committed Dec 31, 2021
1 parent a2f41d8 commit 9b6953d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Admin/Extension/Gedmo/TranslatableAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ public function alterObject(AdminInterface $admin, $object)
return;
}

$objectManager->refresh($object);
$this->setLocale($object, $admin);
}

Expand Down Expand Up @@ -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);
}
}
}
43 changes: 42 additions & 1 deletion tests/Admin/Extension/Gedmo/TranslatableAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand Down
7 changes: 7 additions & 0 deletions tests/Fixtures/Model/ModelTranslatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ class ModelTranslatable implements Translatable
* @var string|null
*/
public $locale = null;

/**
* @ORM\Column(type="string", length=10)
*
* @var string
*/
public $refreshableField = '';
}

0 comments on commit 9b6953d

Please sign in to comment.