Active Record Callbacks
About Callbacks
Callbacks are methods that get called during an object instance's lifecycle. Callbacks happen when an ActiveRecord instance is loaded, created, deleted, or updated.
The following table lists callback triggers, related callback methods and default behavior of the callback methods:
Trigger | Before | After |
---|---|---|
reload() | beforeFind(): do nothing | afterFind(): do nothing |
create() | beforeCreate(): validatesRecordBeforeCreate() | afterCreate(): do nothing |
delete() | beforeDelete(): validatesRecordBeforeDelete() | afterDelete(): do nothing |
update() | beforeUpdate(): validatesRecordBeforeUpdate() | afterUpdate(): do nothing |
updateChanged() | beforeUpdateChanged(): validatesRecordBeforeUpdate() | afterUpdateChanged(): do nothing |
save() | beforeSave(): validatesRecordBeforeSave() | afterSave(): do nothing |
setData() | beforeSetData(): creates a backup copy of the current record data | afterSetData(): records those columns that are modified and sets dirty flag |
Writing Callback Methods
Most of time, the default behavior of callback methods is good enough. If you do need to use your own callback implementation, just simply override those beforeXXX() and afterXXX() methods in the above table.