Add a helper method to refresh all (or some) entities of a given class

[MAILPOET-5845]
This commit is contained in:
Jan Jakes
2024-01-18 17:55:15 +01:00
committed by Aschepikov
parent aa13c8956d
commit 682527cd1b
2 changed files with 92 additions and 0 deletions

View File

@@ -111,6 +111,24 @@ abstract class Repository {
$this->entityManager->refresh($entity);
}
/**
* @param callable(T): bool|null $filter
*/
public function refreshAll(callable $filter = null): void {
$className = $this->getEntityClassName();
$rootClassName = $this->entityManager->getClassMetadata($className)->rootEntityName;
$entities = $this->entityManager->getUnitOfWork()->getIdentityMap()[$rootClassName] ?? [];
foreach ($entities as $entity) {
if (!($entity instanceof $className)) {
continue;
}
if ($filter && !$filter($entity)) {
continue;
}
$this->entityManager->refresh($entity);
}
}
public function flush() {
$this->entityManager->flush();
}