spiralout/Tracks — Issue #5: Repository attempts to save events before applying guid
Author: longjos
Created: 2012-01-22 04:54:31+00:00
State: open

Ran into this issue using the ZendDB EventStorage class.
When Repository::save() is called eventStore->save() attempts to check for an exisiting entity. However because the 'on' handlers are not called until _routeEvents() is called the guid is not set on the aggregate root resulting in an exception.
A work around is to set $this->guid in the 'create' method of the Entity. However this is not proper practice.

``` php
/**
* Save an aggregate root, and call event handlers for all new events
*
* @param AggregateRoot $aggregateRoot An Aggregate Root
*
* @return null
*/
public function save(AggregateRoot $aggregateRoot)
{
if (count($aggregateRoot->getAllAppliedEvents()) == 0) {
return;
}

$this->eventStore->save($aggregateRoot);
$this->_routeEvents($aggregateRoot);
$this->_updateVersionsAndClearEvents($aggregateRoot);
$this->_saveSnapshots($aggregateRoot);
}

```