Behaviours in Models

Agile Toolkit implements hook() and addHook() on a very low level. Any object may define hook() and other objects can place call-backs by previously calling addHook().

Model defines 8 hooks. Four of them are executed before "load", "unload", "delete" and "save" and other four are executed after. beforeLoad, beforeSave and beforeDelete are being passed 2nd argument which is optional "id". Like any other hook, first argument is going to be object hook was placed on. In our case - The Model.

By using those callbacks it's possible to implement additional controllers such as timestamps, owner-stamps, audit logs, field encryption. It's also great that any object can use those callbacks, even model itself.

class Model_User extends Model {




// Validate email before saving
if(!filter_var($m['email'], FILTER_VALIDATE_EMAIL))
$m->exception('Invalid email')->setField('email');

// Encrypt password before save

Controller_Audit extends AbstractController {
defaultValue(date('Y-m-d H:i:s'))->system(true);
$m->loaded())$m['updated_dts']=date('Y-m-d H:i:s');

As you see, hooks can be used for validation, data conversion, timestamps and hooks can be added in many different ways.