Audit Trail & State Rebuild

How to implement Event Sourcing for audit trails and state rebuilds in PHP

The Problem You Recognize

A customer disputes a charge. Your support team asks "what exactly happened to this order?" The answer requires reading application logs, database timestamps, and hoping someone didn't overwrite the data.

Your read models need a schema change. You write a migration script, but there's no way to verify the migrated data is correct — the original events that created it are gone. You store the current state, but not how you got there.

The symptoms:

  • No history — you know what the current price is, but not what it was yesterday

  • Risky migrations — changing the read model means writing one-off scripts and praying

  • Compliance gaps — auditors ask for a complete trail of changes and you can't provide one

What the Industry Calls It

Event Sourcing — instead of storing the current state, store the sequence of events that led to it. Rebuild any view of the data by replaying events. Get a complete, immutable audit trail for free.

How Ecotone Solves It

Ecotone provides Event Sourcing as a first-class feature with built-in projections. Your aggregate records events instead of mutating state:

#[EventSourcingAggregate]
class Order
{
    #[Identifier]
    private string $orderId;

    #[CommandHandler]
    public static function place(PlaceOrder $command): array
    {
        return [new OrderWasPlaced($command->orderId, $command->items)];
    }

    #[EventSourcingHandler]
    public function onOrderPlaced(OrderWasPlaced $event): void
    {
        $this->orderId = $event->orderId;
    }
}

Build read models (projections) that can be rebuilt at any time from the event history:

Works with Postgres, MySQL, and MariaDB for event storage. Projections can write to any storage you choose.

Next Steps

Last updated

Was this helpful?