Events

Events in Spot are powered by the Sabre EventEmitter, and are exposed to your entity via the events static method. This allows spot to load your custom events when your entity’s mapper is first loaded.

Example Entity

<?php
namespace Entity;
use Spot\Entity;
use Spot\Mapper;
use Spot\EventEmitter;
class Post extends \Spot\Entity
{
protected static $table = 'posts';
public static function fields()
{
return [
'id' => ['type' => 'integer', 'autoincrement' => true, 'primary' => true],
'title' => ['type' => 'string', 'required' => true],
'body' => ['type' => 'text', 'required' => true],
'status' => ['type' => 'integer', 'default' => 0, 'index' => true],
'author_id' => ['type' => 'integer', 'required' => true],
'is_active' => ['type' => 'boolean', 'index' => true],
'date_created' => ['type' => 'datetime', 'value' => new \DateTime()]
];
}
public static function events(EventEmitter $eventEmitter)
{
$eventEmitter->on('beforeSave', function (Entity $entity, Mapper $mapper) {
// Ensure certain statuses are not active
if ($entity->status === 9) {
$entity->is_active = false;
}
});
}
}
In addition to the on method used here, the EventEmitter also has a once method that will ensure your callback is only executed one time.

Events

beforeSave

Called before any save event through any method that saves an entity.

Returning false from your event callback will not save the entity.

afterSave

Called after any save event through any method that saves an entity.

beforeInsert

Called before a new entity is inserted via the save, insert or create method.

Returning false from your event callback will not insert the entity.

afterInsert

Called after a new entity is inserted via the save, insert or create method.

beforeUpdate

Called before a new entity is updated via the save or update method.

Returning false from your event callback will not update the entity.

afterUpdate

Called after a new entity is updated via the save or update method.

beforeValidate

Called before validation is run. Triggered by every save event through any method that saves an entity.

Returning false from your event callback will trigger an immediate validation failure, will not run any other validations, and will not save the entity.

afterValidate

Called after validation is run. Triggered by every save event through any method that saves an entity.

Returning false from your event callback will trigger a validation failure, and will not save the entity.