Use refreshAll() helper for batch updates

[MAILPOET-5845]
This commit is contained in:
Jan Jakes
2024-01-18 17:59:06 +01:00
committed by Aschepikov
parent d6da2af55b
commit a947dd36cd
3 changed files with 26 additions and 2 deletions

View File

@@ -69,12 +69,19 @@ class FormsRepository extends Repository {
return 0; return 0;
} }
return $this->entityManager->createQueryBuilder() $result = $this->entityManager->createQueryBuilder()
->update(FormEntity::class, 'f') ->update(FormEntity::class, 'f')
->set('f.deletedAt', 'CURRENT_TIMESTAMP()') ->set('f.deletedAt', 'CURRENT_TIMESTAMP()')
->where('f.id IN (:ids)') ->where('f.id IN (:ids)')
->setParameter('ids', $ids) ->setParameter('ids', $ids)
->getQuery()->execute(); ->getQuery()->execute();
// update was done via DQL, make sure the entities are also refreshed in the entity manager
$this->refreshAll(function (FormEntity $entity) use ($ids) {
return in_array($entity->getId(), $ids, true);
});
return $result;
} }
public function bulkRestore(array $ids): int { public function bulkRestore(array $ids): int {
@@ -82,13 +89,20 @@ class FormsRepository extends Repository {
return 0; return 0;
} }
return $this->entityManager->createQueryBuilder() $result = $this->entityManager->createQueryBuilder()
->update(FormEntity::class, 'f') ->update(FormEntity::class, 'f')
->set('f.deletedAt', ':deletedAt') ->set('f.deletedAt', ':deletedAt')
->where('f.id IN (:ids)') ->where('f.id IN (:ids)')
->setParameter('deletedAt', null) ->setParameter('deletedAt', null)
->setParameter('ids', $ids) ->setParameter('ids', $ids)
->getQuery()->execute(); ->getQuery()->execute();
// update was done via DQL, make sure the entities are also refreshed in the entity manager
$this->refreshAll(function (FormEntity $entity) use ($ids) {
return in_array($entity->getId(), $ids, true);
});
return $result;
} }
public function bulkDelete(array $ids): int { public function bulkDelete(array $ids): int {

View File

@@ -94,6 +94,11 @@ class ScheduledTaskSubscribersRepository extends Repository {
->setParameter('task', $task) ->setParameter('task', $task)
->getQuery() ->getQuery()
->execute(); ->execute();
// update was done via DQL, make sure the entities are also refreshed in the entity manager
$this->refreshAll(function (ScheduledTaskSubscriberEntity $entity) use ($task, $subscriberIds) {
return $entity->getTask() === $task && in_array($entity->getSubscriberId(), $subscriberIds, true);
});
} }
$this->checkCompleted($task); $this->checkCompleted($task);

View File

@@ -308,6 +308,11 @@ class ScheduledTasksRepository extends Repository {
->setParameter('ids', $ids, Connection::PARAM_INT_ARRAY) ->setParameter('ids', $ids, Connection::PARAM_INT_ARRAY)
->getQuery() ->getQuery()
->execute(); ->execute();
// update was done via DQL, make sure the entities are also refreshed in the entity manager
$this->refreshAll(function (ScheduledTaskEntity $entity) use ($ids) {
return in_array($entity->getId(), $ids, true);
});
} }
/** /**