Event handling using an event handler CFC

An application-wide event handler CFC can be defined to handle callback when *any* entity is inserted, updated, deleted, or retrieved. This CFC must be configured at the application level as an ORM setting:

ormsettings.evenHandler="X.Y.EventHandler"

The event handler CFC needs to implement the CFIDE.ORM.IEventHandler interface. This CFC gets the callbacks from all persistence-related events and handles them accordingly. In this case, a single CFC handles the events for all the CFCs.

This interface contains the following methods for the event handler CFC:

For application-wide event handler CFC, you need to specify the component name also along with other arguments.

The methods for application-wide event handler are:

  • preLoad(entity): This method is called before the load operation or before the data is loaded from the database.

  • postLoad(entity): This method is called after the load operation is complete.

  • preInsert(entity): This method is called just before the object is inserted. The insert operation is vetoed if this method returns "true".

  • postInsert(entity): This method is called after the insert operation is complete.

  • preUpdate(entity, Struct oldData): This method is called just before the object is updated. A struct of old data is passed to this method to know the original state of the entity being updated. The update operation is vetoed if this method returns "true".

    Note: When you call the EntitySave() method on an object that is not loaded using EntityLoad(), it gets updated but the intercepter call fails. This happens because an empty map is created for the object and there is no previous data associated with it.
  • postUpdate(entity): This method is called after the update operation is complete.

  • preDelete(entity): This method is called before the object is deleted. The delete operation is vetoed if this method returns "true".

  • postDelete(entity): This method is called after the delete operation is complete.

Note: If event handlers are defined in both persistent CFC and event handler CFC, the persistent CFC is given the callback before calling the application wide event handler.