add flush option in the entity manager on create, update, remove, and persist methods

This commit is contained in:
Simon Vieille 2023-09-28 10:31:00 +02:00
parent a08c62229d
commit 7fceefa6d3
Signed by: deblan
GPG key ID: 579388D585F70417
2 changed files with 15 additions and 8 deletions

View file

@ -6,6 +6,7 @@
* add `addForcedFilterHandler` method in repository query * add `addForcedFilterHandler` method in repository query
* add `inline_form_validation` option to validate inline forms with custom algo * add `inline_form_validation` option to validate inline forms with custom algo
* add crud sorting in the session * 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 ## [1.21.1] - 2023-08-17
### Added ### Added

View file

@ -25,13 +25,13 @@ class EntityManager
$this->entityManager = $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) { if ($dispatchEvent) {
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_CREATE_EVENT); $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_CREATE_EVENT);
} }
$this->persist($entity); $this->persist($entity, $flush);
if ($dispatchEvent) { if ($dispatchEvent) {
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT); $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT);
@ -40,13 +40,13 @@ class EntityManager
return $this; 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) { if ($dispatchEvent) {
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_UPDATE_EVENT); $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_UPDATE_EVENT);
} }
$this->persist($entity); $this->persist($entity, $flush);
if ($dispatchEvent) { if ($dispatchEvent) {
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::UPDATE_EVENT); $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::UPDATE_EVENT);
@ -55,14 +55,17 @@ class EntityManager
return $this; 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) { if ($dispatchEvent) {
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_DELETE_EVENT); $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_DELETE_EVENT);
} }
$this->entityManager->remove($entity); $this->entityManager->remove($entity);
$this->flush();
if ($flush) {
$this->flush();
}
if ($dispatchEvent) { if ($dispatchEvent) {
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::DELETE_EVENT); $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::DELETE_EVENT);
@ -90,9 +93,12 @@ class EntityManager
return $this->entityManager; return $this->entityManager;
} }
protected function persist(EntityInterface $entity) protected function persist(EntityInterface $entity, bool $flush = true)
{ {
$this->entityManager->persist($entity); $this->entityManager->persist($entity);
$this->flush();
if ($flush) {
$this->flush();
}
} }
} }