/** * @param User $user * @return void */ public function save(User $user) { $eventId = new EventStreamId($user->id(), $user->mutatedVersion()); $this->eventStore->appendWith($eventId, $user->mutatingEvents()); }
} /** * @param User $user * @return void */ public function save(User $user) { $eventId = new EventStreamId($user->id(), $user->mutatedVersion()); $this->eventStore->appendWith($eventId, $user->mutatingEvents()); } } namespace Suin\EventSourcing\Application; use PDO; use Suin\EventSourcing\Example\Domain\Model\User\EventStoreUserRepository; use Suin\EventSourcing\Example\Domain\Model\User\User; use Suin\EventSourcing\MySQLPDOEventStore; require_once __DIR__ . '/vendor/autoload.php'; $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root', [PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_AUTOCOMMIT => true]); $pdo->exec('DROP TABLE IF EXISTS tbl_es_event_store'); $pdo->exec(file_get_contents(__DIR__ . '/src/Suin/EventSourcing/Resources/event_store.sql')); $eventStore = new MySQLPDOEventStore($pdo); $userRepository = new EventStoreUserRepository($eventStore); // Persistence example in Application Service $userAggregateId = $userRepository->nextIdentity(); $user = new User($userAggregateId, 'Alice', '*****@*****.**'); $user->changeEmail('*****@*****.**'); $userRepository->save($user); // Query example in Application Service $user = $userRepository->userOfId($userAggregateId); var_dump($user);