Sons of PHP
Source Code
  • 🏠Home
  • Contributor Covenant Code of Conduct
  • Getting Help
  • Reporting Issues
  • Security Policy
  • 🪈Bard
    • Overview
    • Commands
  • Symfony Bundles
    • Feature Toggle
  • Contracts
    • Contracts Overview
    • Common
    • Cookie
    • CQRS
    • Filesystem
    • Mailer
    • Pager
    • Registry
    • State Machine
  • 📦Components
    • Assert
    • Cache
      • Adapters
      • Marshallers
    • Clock
    • Container
    • Cookie
    • CQRS
    • Event Dispatcher
    • Event Sourcing
      • Aggregates
        • Aggregate Repository
      • Event Messages
        • Using the Serializable Event Message
        • Message Enrichers
        • Message Serializers
        • Message Repository
        • Message Upcasters
    • Feature Toggle
    • Filesystem
      • Adapters
    • Http Factory
    • Http Handler
    • Http Message
    • JSON
    • Link
    • Logger
      • Handlers
      • Enrichers
      • Filters
      • Formatters
    • Mailer
      • Transports
    • Money
      • Currency Providers
      • Operators
      • Queries
    • Pager
      • Adapters
    • Registry
    • State Machine
    • Version
  • 💁Contributing
    • Contributing Overview
    • Contributing Code
    • Discussions
    • Documentation
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Components
  2. Event Sourcing
  3. Aggregates

Aggregate Repository

The AggregateRepository is the main interface used to persist and find aggregates.

Usage

<?php
use SonsOfPHP\Component\EventSourcing\Aggregate\Repository\AggregateRepository;
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateIdInterface;
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateInterface;
use SonsOfPHP\Component\EventSourcing\Message\Repository\MessageRepositoryInterface;
use Psr\EventDispatcher\EventDispatcherInterface;

$repository = new AggregateRepository(
    $aggregateClass, // @var string
    $eventDispatcher, // @var EventDispatcherInterface
    $messageRepository // @var MessageRepositoryInterface
);

// @var AggregateIdInterface    $aggregateId
// @var AggregateInterface|null $aggregate
$aggregate = $repository->find($aggregateId);

// You can also pass in a string as the $aggregateId
$aggregate = $repository->find('unique-id');

// To save an aggregate
$repository->persist($aggregate);
PreviousAggregatesNextEvent Messages

Last updated 8 months ago

Was this helpful?

📦