class CallBack (View source)

Callbacks allow the programmer to hook into the life cycle of a {@link Model}.

You can control the state of your object by declaring certain methods to be called before or after methods are invoked on your object inside of ActiveRecord.

Valid callbacks are:

  • after_construct: called after a model has been constructed
  • before_save: called before a model is saved
  • after_save: called after a model is saved
  • before_create: called before a NEW model is to be inserted into the database
  • after_create: called after a NEW model has been inserted into the database
  • before_update: called before an existing model has been saved
  • after_update: called after an existing model has been saved
  • before_validation: called before running validators
  • after_validation: called after running validators
  • before_validation_on_create: called before validation on a NEW model being inserted
  • after_validation_on_create: called after validation on a NEW model being inserted
  • before_validation_on_update: see above except for an existing model being saved
  • after_validation_on_update: ...
  • before_destroy: called after a model has been deleted
  • after_destroy: called after a model has been deleted

This class isn't meant to be used directly. Callbacks are defined on your model like the example below:

class Person extends ActiveRecord\Model { static $before_save = array('make_name_uppercase'); static $after_save = array('do_happy_dance');

public function make_name_uppercase() { $this->name = strtoupper($this->name); }

public function do_happy_dance() { happy_dance(); } }

Available options for callbacks:

  • prepend: puts the callback at the top of the callback chain instead of the bottom

Properties

static protected array $VALID_CALLBACKS List of available callbacks.

Methods

__construct(string $model_class_name)

Creates a CallBack.

array
get_callbacks($name)

Returns all the callbacks registered for a callback type.

mixed
invoke(string $model, string $name, boolean $must_exist = true)

Invokes a callback.

void
register(string $name, mixed $closure_or_method_name = null, array $options = array())

Register a new callback.

Details

CallBack __construct(string $model_class_name)

Creates a CallBack.

Parameters

string $model_class_name The name of a {@link Model} class

Return Value

CallBack

array get_callbacks($name)

Returns all the callbacks registered for a callback type.

Parameters

$name string Name of a callback (see {@link VALID_CALLBACKS $VALID_CALLBACKS})

Return Value

array array of callbacks or null if invalid callback name.

mixed invoke(string $model, string $name, boolean $must_exist = true)

Invokes a callback.

Parameters

string $model Model to invoke the callback on.
string $name Name of the callback to invoke
boolean $must_exist Set to true to raise an exception if the callback does not exist.

Return Value

mixed null if $name was not a valid callback type or false if a method was invoked that was for a before_* callback and that method returned false. If this happens, execution of any other callbacks after the offending callback will not occur.

void register(string $name, mixed $closure_or_method_name = null, array $options = array())

Register a new callback.

The option array can contain the following parameters:

  • prepend: Add this callback at the beginning of the existing callbacks (true) or at the end (false, default)

Parameters

string $name Name of callback type (see {@link VALID_CALLBACKS $VALID_CALLBACKS})
mixed $closure_or_method_name Either a closure or the name of a method on the {@link Model}
array $options Options array

Return Value

void

Exceptions

ActiveRecordException if invalid callback type or callback method was not found