From 7fceefa6d358943532d516957caa1d6313bf21ad Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Thu, 28 Sep 2023 10:31:00 +0200 Subject: [PATCH] add flush option in the entity manager on create, update, remove, and persist methods --- CHANGELOG.md | 1 + src/core/Manager/EntityManager.php | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22f7647..a524f2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * add `addForcedFilterHandler` method in repository query * add `inline_form_validation` option to validate inline forms with custom algo * add crud sorting in the session +* add flush option in the entity manager on create, update, remove, and persist methods ## [1.21.1] - 2023-08-17 ### Added diff --git a/src/core/Manager/EntityManager.php b/src/core/Manager/EntityManager.php index c4eb80e..c642053 100644 --- a/src/core/Manager/EntityManager.php +++ b/src/core/Manager/EntityManager.php @@ -25,13 +25,13 @@ class EntityManager $this->entityManager = $entityManager; } - public function create(EntityInterface $entity, bool $dispatchEvent = true): self + public function create(EntityInterface $entity, bool $dispatchEvent = true, bool $flush = true): self { if ($dispatchEvent) { $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_CREATE_EVENT); } - $this->persist($entity); + $this->persist($entity, $flush); if ($dispatchEvent) { $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT); @@ -40,13 +40,13 @@ class EntityManager return $this; } - public function update(EntityInterface $entity, bool $dispatchEvent = true): self + public function update(EntityInterface $entity, bool $dispatchEvent = true, bool $flush = true): self { if ($dispatchEvent) { $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_UPDATE_EVENT); } - $this->persist($entity); + $this->persist($entity, $flush); if ($dispatchEvent) { $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::UPDATE_EVENT); @@ -55,14 +55,17 @@ class EntityManager return $this; } - public function delete(EntityInterface $entity, bool $dispatchEvent = true): self + public function delete(EntityInterface $entity, bool $dispatchEvent = true, bool $flush = true): self { if ($dispatchEvent) { $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_DELETE_EVENT); } $this->entityManager->remove($entity); - $this->flush(); + + if ($flush) { + $this->flush(); + } if ($dispatchEvent) { $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::DELETE_EVENT); @@ -90,9 +93,12 @@ class EntityManager return $this->entityManager; } - protected function persist(EntityInterface $entity) + protected function persist(EntityInterface $entity, bool $flush = true) { $this->entityManager->persist($entity); - $this->flush(); + + if ($flush) { + $this->flush(); + } } }