3.7.7 Callback Methods

If you want to sneak in some logic just before or after a CakePHP model operation, use model callbacks. These functions can be defined in model classes (including your AppModel) class. Be sure to note the expected return values for each of these special functions.

3.7.7.1 beforeFind

beforeFind(mixed $queryData)

Called before any find-related operation. The $queryData passed to this callback contains information about the current query: conditions, fields, etc.

If you do not wish the find operation to begin (possibly based on a decision relating to the $queryData options), return false. Otherwise, return the possibly modified $queryData, or anything you want to get passed to find and its counterparts.

You might use this callback to restrict find operations based on a user’s role, or make caching decisions based on the current load.

3.7.7.2 afterFind

afterFind(array $results, bool $primary)

Use this callback to modify results that have been returned from a find operation, or to perform any other post-find logic. The $results parameter passed to this callback contains the returned results from the model's find operation, i.e. something like:

$results = array(
  0 => array(
    'ModelName' => array(
      'field1' => 'value1',
      'field2' => 'value2',
    ),
  ),
);
  1. $results = array(
  2. 0 => array(
  3. 'ModelName' => array(
  4. 'field1' => 'value1',
  5. 'field2' => 'value2',
  6. ),
  7. ),
  8. );

The return value for this callback should be the (possibly modified) results for the find operation that triggered this callback.

If $primary is false, the format of $results will be a little different than one might expect; instead of the result you would normally get from a find operation, you will get this:

$results = array(
  'field_1' => 'value',
  'field_2' => 'value2'
);
  1. $results = array(
  2. 'field_1' => 'value',
  3. 'field_2' => 'value2'
  4. );

Code expecting $primary to be true will probably get a "Cannot use string offset as an array" fatal error from PHP if a recursive find is used.

3.7.7.3 beforeValidate

beforeValidate()

Use this callback to modify model data before it is validated. It can also be used to add additional, more complex validation rules using Model::invalidate(). In this context, model data is accessible via $this->data. This function must also return true, otherwise the current save() execution will abort.

3.7.7.4 beforeSave

beforeSave()

Place any pre-save logic in this function. This function executes immediately after model data has been successfully validated, but just before the data is saved. This function should also return true if you want the save operation to continue.

This callback is especially handy for any data-massaging logic that needs to happen before your data is stored. If your storage engine needs dates in a specific format, access it at $this->data and modify it.

3.7.7.5 afterSave

afterSave(boolean $created)

If you have logic you need to be executed just after every save operation, place it in this callback method.

The value of $created will be true if a new object was created (rather than an update).

3.7.7.6 beforeDelete

beforeDelete()

Place any pre-deletion logic in this function. This function should return true if you want the deletion to continue, and false if you want to abort.

3.7.7.7 afterDelete

afterDelete()

Place any logic that you want to be executed after every deletion in this callback method.

3.7.7.8 onError

onError()

Called if any problems occur.