abstract class Athena::ORM::Events::EventArgs
inherits Athena::Contracts::EventDispatcher::Event
#
Base type of every event the ORM emits.
Events reach code in two ways:
- Lifecycle callbacks: methods on an entity annotated with
AORMA::PrePersist,AORMA::PostPersist,AORMA::PreUpdate,AORMA::PostUpdate,AORMA::PreRemove,AORMA::PostRemoveorAORMA::PreFlush. They are invoked for instances of that entity only, and may accept the event as their only argument. - The event dispatcher passed to
AORM::EntityManager.neworAORM::EntityManagerFactory.new. It receives the events concerning the entity manager as a whole:AORM::Events::PreFlushEventArgs,AORM::Events::OnFlushEventArgs,AORM::Events::PostFlushEventArgsandAORM::Events::OnClearEventArgs.
@[AORMA::Entity]
class Article < AORM::Entity
@[AORMA::Column]
@[AORMA::ID]
@[AORMA::GeneratedValue]
property! id : Int64
@[AORMA::Column]
property! updated_at : Time
# Callbacks may omit the event argument.
@[AORMA::PrePersist]
@[AORMA::PreUpdate]
def touch : Nil
@updated_at = Time.utc
end
# Or accept it, typed as the event for this entity.
@[AORMA::PostPersist]
def log_insert(event : AORM::Events::PostPersistEventArgs(Article)) : Nil
Log.info { "Inserted article #{event.entity.id}" }
end
end
Lifecycle callbacks may also be declared in a module, and apply to every entity that includes it. More than one callback may be registered for the same event.
Warning
Calling AORM::EntityManager#flush from within an event that is emitted by a flush re-enters the unit of work while it is committing, which isn't supported.
Todo
Per-entity events aren't sent to the event dispatcher yet, and entity listener classes aren't supported; use lifecycle callbacks instead.
Direct known subclasses
Athena::ORM::Events::LifecycleEventArgs(T)
Athena::ORM::Events::ManagerEventArgs
Athena::ORM::Events::OnClearEventArgs