Skip to content

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:

@[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