Maslosoft Mangan Logo Maslosoft Mangan Documentation
MongoDB Object Persister

Edit

Events

Events can be attached to model instance, class name, interface name or even trait name.

When using class names, it is recommended to use ::class magic constant.

Function on()

Attaches an event handler to a class-level event.

When a class-level event is triggered, event handlers attached to that class and all parent classes will be invoked.

For example, the following code attaches an event handler to document's afterInsert event:

Event::on($model, EntityManager::EventAfterInsert, function ($event) {
        var_dump(get_class($event->sender) . ' is inserted.');
});

The handler will be invoked for every successful document insertion.

NOTE: Each call will attach new event handler. When placing event initialization in class constructors etc. ensure that it is evaluated once, or it might trigger same event handler multiple times.

When the event handler is invoked, this data can be accessed via [[Event::data]].

handler list. If false, the new handler will be inserted at the beginning of the existing handler list.

Function off()

Detaches an event handler from a class-level event.

This method is the opposite of [[on()]].

If it is null, all handlers attached to the named event will be removed.

Function trigger()

Triggers a class-level event. This method will cause invocation of event handlers that are attached to the named event for the specified class and all its parent classes.

More on events: