Skip to content

Commit

Permalink
Replace all found ReplaceableEvents
Browse files Browse the repository at this point in the history
In the case of a domain aggregate recording multiple instances of the same ReplaceableEvent, we need to remove ALL found instances of this ReplaceableEvent for the matching aggregateRoot. Otherwise, only one is replaced and the rest remain as duplicate events in the event store.
  • Loading branch information
benr77 authored Sep 26, 2020
1 parent 5c9d479 commit 037d990
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Doctrine/DoctrineEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public function append(DomainEvent $domainEvent): void

public function replace(DomainEvent $domainEvent): void
{
$previous = $this->repository->findOneBy([
$replaceableEvents = $this->repository->findBy([
'aggregateRoot' => $domainEvent->getAggregateRootId(),
'typeName' => get_class($domainEvent),
'publishedOn' => null,
]);

if ($previous) {
$this->em->remove($previous);
foreach ($replaceableEvents as $replaceableEvent) {
$this->em->remove($replaceableEvent);
}

$this->append($domainEvent);
Expand Down

0 comments on commit 037d990

Please sign in to comment.