Replace deprecated UnitOfWork::clear($entityClassName) with a more granular refresh

We removed EntityManager::clear($entityClassName) already, but this one is deprecated
as well: 212edaa80b/lib/Doctrine/ORM/UnitOfWork.php (L2762)

[MAILPOET-5745]
This commit is contained in:
Jan Jakes
2023-11-30 15:09:38 +01:00
committed by Aschepikov
parent 125b0ab03d
commit a54fd9b8fc
2 changed files with 65 additions and 3 deletions

View File

@ -6,13 +6,22 @@ use MailPoet\Doctrine\Repository;
use MailPoet\Entities\SettingEntity;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Carbon\Carbon;
use MailPoetVendor\Doctrine\ORM\Query;
/**
* @extends Repository<SettingEntity>
*/
class SettingsRepository extends Repository {
public function findOneByName($name) {
return $this->doctrineRepository->findOneBy(['name' => $name]);
public function findOneByName(string $name): ?SettingEntity {
// Always fetch fresh entity data (= don't use "findOneBy()"). See also further below.
$result = (array)$this->doctrineRepository->createQueryBuilder('s')
->where('s.name = :name')
->setParameter('name', $name)
->getQuery()
->setHint(Query::HINT_REFRESH, true)
->getResult();
return isset($result[0]) && $result[0] instanceof SettingEntity ? $result[0] : null;
}
public function createOrUpdateByName($name, $value) {
@ -30,7 +39,11 @@ class SettingsRepository extends Repository {
'value' => is_array($value) ? serialize($value) : $value,
'now' => $now,
]);
$this->entityManager->getUnitOfWork()->clear(SettingEntity::class);
// Ensure entity data is up-to-date in memory.
// - We can't use "refresh()"; we don't have the entity instance, and it can be a new record.
// - We can't use "findOneBy()"; it could return a cached entity instance.
$this->findOneByName($name);
}
protected function getEntityClassName() {

View File

@ -0,0 +1,49 @@
<?php declare(strict_types = 1);
namespace MailPoet\Test\Settings;
use MailPoet\Entities\SettingEntity;
use MailPoet\Settings\SettingsRepository;
use MailPoetTest;
class SettingsRepositoryTest extends MailPoetTest {
public function testItFetchesAlwaysFreshData(): void {
$repository = $this->diContainer->get(SettingsRepository::class);
$setting = new SettingEntity();
$setting->setName('name');
$setting->setValue('value');
$this->entityManager->persist($setting);
$this->entityManager->flush();
$this->assertSame('value', $setting->getValue());
$this->entityManager->createQueryBuilder()
->update(SettingEntity::class, 's')
->set('s.value', ':value')
->where('s.name = :name')
->setParameter('value', 'new value')
->setParameter('name', 'name')
->getQuery()
->execute();
$this->assertSame('value', $setting->getValue());
$newSetting = $repository->findOneByName('name');
$this->assertSame($setting, $newSetting);
$this->assertSame('new value', $setting->getValue());
$this->assertSame('new value', $newSetting->getValue());
}
public function testUpdateRefreshesExistingData(): void {
$repository = $this->diContainer->get(SettingsRepository::class);
$setting = new SettingEntity();
$setting->setName('name');
$setting->setValue('value');
$this->entityManager->persist($setting);
$this->entityManager->flush();
$this->assertSame('value', $setting->getValue());
$repository->createOrUpdateByName('name', 'new value');
$this->assertSame('new value', $setting->getValue());
}
}