Events
This library includes several events that can be broadcast, and a trait you can use in your DAOs to broadcast these events.
Event Classes
The following classes are all in the Caridea\Dao\Event
namespace. They all extend from the Caridea\Dao\Event
class, which includes a method: getEntity
that returns the entity involved.
PreDelete
– To be broadcast before a delete eventPostDelete
– To be broadcast after a delete eventPreInsert
– To be broadcast before an insert eventPostInsert
– To be broadcast after an insert eventPreUpdate
– To be broadcast before an update eventPostUpdate
– To be broadcast after an update event
Publishing Trait
The Caridea\Dao\Event\Publishing
trait has methods your DAO can call that publish the above events. They all accept a single argument that is the entity involved.
preDelete
– publishes aPreDelete
eventpostDelete
– publishes aPostDelete
eventpreInsert
– publishes aPreInsert
eventpostInsert
– publishes aPostInsert
eventpreUpdate
– publishes aPreUpdate
eventpostUpdate
– publishes aPostUpdate
event
Here's an example class that uses this trait:
class MyDao extends \Caridea\Dao\MongoDb implements \Caridea\Event\PublisherAware
{
use \Caridea\Dao\Event\Publishing;
public function create($record)
{
$this->preInsert($record);
$this->doExecute(function () {
// do some persistence magic
});
$this->postInsert($record);
}
}
When creating an instance of this class, you'd want to make sure to call setPublisher
, or better yet, use caridea/container to factory your DAO.
But My Persistence Library Has Events…
Awesome! Don't use this trait, then! If your library (e.g. Doctrine) already handles events that occur to entities, then this trait becomes irrelevant.