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() {