As known from the Doctrine ORM project, this library makes use of Events.
- prePersist - Fires before an entity is persisted
- postPersist - Fires after an entity is persisted
- preRelationCreate - Fires before a relation is created
- postRelationCreate - Fires after a relation is created
- preStmtExecute - Fires before a statement (query) is executed
- postStmtExecute - Fires after a statement (query) is executed
- preRemove - Fires before an entity is removed
- postRemove - Fires after an entity is removed
- preRelationRemove - Fires before a relation is removed
- postRelationRemove - Fires after a relation is removed
The usage of those events is quiet simple. First you have to create an EventListener. This is a class containing
functions, named after the events. If you want to listen for prePersist
for example, your listener looks like this:
use HireVoice\Neo4j\Event as Events;
class PrePersistListener
{
public function prePersist(Events\PrePersist $event)
{
$entity = $event->getEntity();
// do your stuff here...
}
}
Of course, you can listen to more then one event. See the full example below.
In a second step, the listener must be passed to an EventManager instance. After the listener was added, inject the EventManager into the EntityManager:
use \Doctrine\Common\EventManager;
// ...
$eventManager = new EventManager();
$listener = new PrePersistListener();
$eventManager->addEventListener(
array('prePersist'), // array of all listened events
$listener // instance of your event listener
);
$entityManager->setEventManager($eventManager);
Heads Up: This wire-up should usually be done using a Dependency Injection Container...
The implementation bases on the Doctrine Common API Event Manager. More documentation there...
use HireVoice\Neo4j\Event as Events;
class ExampleEventListener
{
public function prePersist(Events\PrePersist $event)
{
return null;
}
public function postPersist(Events\PostPersist $event)
{
return null;
}
public function preRelationCreate(Events\PreRelationCreate $event)
{
return null;
}
public function postRelationCreate(Events\PostRelationCreate $event)
{
return null;
}
public function preStmtExecute(Events\PreStmtExecute $event)
{
return null;
}
public function postStmtExecute(Events\PostStmtExecute $event)
{
return null;
}
public function preRemove(Events\PreRemove $event)
{
return null;
}
public function postRemove(Events\PostRemove $event)
{
return null;
}
public function preRelationRemove(Events\PreRelationRemove $event)
{
return null;
}
public function postRelationRemove(Events\PostRelationRemove $event)
{
return null;
}
}