PHPackages                             malocher/zf2-event-store-module - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. malocher/zf2-event-store-module

ActiveLibrary

malocher/zf2-event-store-module
===============================

EventStore integration for Zend Framework 2

026PHP

Since Jan 26Pushed 12y ago2 watchersCompare

[ Source](https://github.com/malocher/zf2-event-store-module)[ Packagist](https://packagist.org/packages/malocher/zf2-event-store-module)[ RSS](/packages/malocher-zf2-event-store-module/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

MalocherEventStoreModule
========================

[](#malochereventstoremodule)

Zend Framework 2 Module that integrates the [Malocher EventStore](https://github.com/malocher/event-store) in your ZF2 application.

[![Build Status](https://camo.githubusercontent.com/6be826dc7c2d5eff8afb68ccd266974a68d7ce6df348c1cca6abcd9ac3934397/68747470733a2f2f7472617669732d63692e6f72672f6d616c6f636865722f7a66322d6576656e742d73746f72652d6d6f64756c652e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/malocher/zf2-event-store-module)

Installation
------------

[](#installation)

Installation of MalocherEventStoreModule uses composer. For composer documentation, please refer to [getcomposer.org](http://getcomposer.org/). Add following requirement to your composer.json

```
"malocher/zf2-event-store-module" : "dev-master"
```

Then add `MalocherEventStoreModule` to your `config/application.config.php`

Installation without composer is not officially supported, and requires you to install and autoload the dependencies specified in the `composer.json`.

Setup
-----

[](#setup)

Setup the EventStore using your module or application configuration. Put all EventStore options under the key `malocher.eventstore`.

```
  'malocher.eventstore' => array(
        ...
  )
```

The MalocherEventStoreModule ships with an [Zf2EventStoreAdapter](https://github.com/malocher/zf2-event-store-module/blob/master/src/MalocherEventStoreModule/Adapter/Zf2EventStoreAdapter.php). You can setup the ZF2 DB Adapter via the `malocher.eventstore.adapter.connection` configuration. Use the same adapter options like you would do with a normal ZF2 DB Adapter. Checkout [ZF2 docs](http://framework.zend.com/manual/2.1/en/modules/zend.db.adapter.html#creating-an-adapter-quickstart)for all available options. Here is an example that setup a SQLite in memory adapter:

```
'malocher.eventstore' => array(
    'adapter' => array(
        'MalocherEventStoreModule\Adapter\Zf2EventStoreAdapter' => array(
            'connection' => array(
                'driver' => 'Pdo_Sqlite',
                'database' => ':memory:'
            )
        )
    ),
)
```

Right now you have to create the database schema yourself. We are working on a command line tool to help you setup your EventStore. The store needs one `snapshot` table and an `event stream` table per EventSourcedObject (AggregateRoot, if your using DDD).

Here is the example schema used for SQLite:

```
-- create one snapshot table
CREATE TABLE snapshot
(
    id INTEGER PRIMARY KEY,
    sourceType TEXT,
    sourceId  TEXT,
    snapshotVersion INTEGER
)

-- create a table for each of your EventSourcedObjects
-- by default My\Namespaced\Object leads to the table name object_stream
CREATE TABLE _stream
(
    eventId TEXT PRIMARY KEY,
    sourceId TEXT,
    sourceVersion INTEGER,
    eventClass TEXT,
    payload TEXT,
    eventVersion REAL,
    timestamp INTEGER
)
```

Usage
-----

[](#usage)

Use the ZF2 ServiceManger to get an instance of the Malocher\\EventStore. A ServiceFactory passes your malocher.eventstore configuration to the EventStore, so it is ready to save and load your EventSourcedObjects.

```
use Malocher\EventStore\EventStore;
use Malocher\EventStore\StoreEvent\PostPersistEvent;

/* @var $eventStore EventStore */
$eventStore = $serviceManager->get('malocher.eventstore');

//You can listen to the eventstore.events_post_persist event to be notified
//when an object was changed
$eventstore->events()->attach(PostPersistEvent::NAME, function(PostPersistEvent $e) {
    foreach ($e->getPersistedEvents() as $persitedEvent) {
        if ($persistedEvent instanceof \My\Event\UserNameChangedEvent) {
            $oldName = $persistedEvent->getPayload()['oldName'];
            $newName = $persistedEvent->getPayload()['newName'];
            echo "Name changed from $oldName to $newName";
        }
    }
});

$userRepository = $eventStore->getRepository('My\User');

//Assuming username is the primary key
$user = $userRepository->find('John');

$user->changeName('Malocher');

$userRepository->save($user);

//Output: Name changed from John to Malocher
```

ZF2 integration
---------------

[](#zf2-integration)

In the last example we've shown how to listen to the PostPersistEvent of the EventStore. We've used `$eventStore->events()->attach(...)`, which is an important difference to the original EventStore implementation. By default the Malocher EventStore uses a Symfony EventDispatcher, but for the ZF2 version we've replaced it with an ZF2 EventManagerProxy. You can add listeners the ZF2 way or the Symfony way, it's up to you. The good news is, that you can work with the flexible concepts of SharedEventCollections by attaching a listener via ZF2 SharedEventManager.

```
use Malocher\EventStore\EventStore;
use Malocher\EventStore\StoreEvent\PostPersistEvent;

//in a module bootstrap
$sharedEventManager = $serviceManager->get('SharedEventManager');

$sharedEventManager->attach(
    'EventStore',
    PostPersistEvent::NAME,
    function(PostPersistEvent $e) {
    foreach ($e->getPersistedEvents() as $persitedEvent) {
        if ($persistedEvent instanceof \My\Event\UserNameChangedEvent) {
            $oldName = $persistedEvent->getPayload()['oldName'];
            $newName = $persistedEvent->getPayload()['newName'];
            echo "Name changed from $oldName to $newName";
        }
    }
);

//somewhere else in your application ...

$userRepository = $eventStore->getRepository('My\User');

//Assuming username is the primary key
$user = $userRepository->find('John');

$user->changeName('Malocher');

$userRepository->save($user);

//Output: Name changed from John to Malocher
```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/101de38f52eab4a997505eb6bb4e2330351e1baf8c49bec51d466c16aabfc8ff?d=identicon)[Malocher](/maintainers/Malocher)

### Embed Badge

![Health badge](/badges/malocher-zf2-event-store-module/health.svg)

```
[![Health](https://phpackages.com/badges/malocher-zf2-event-store-module/health.svg)](https://phpackages.com/packages/malocher-zf2-event-store-module)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
