Skip to content

Commit

Permalink
Update Model/Behavior callback documentation to be truthful.
Browse files Browse the repository at this point in the history
  • Loading branch information
bar committed Sep 10, 2013
1 parent ccb1d5d commit c50da87
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
41 changes: 24 additions & 17 deletions en/models/behaviors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ Creating Behaviors

Behaviors that are attached to Models get their callbacks called
automatically. The callbacks are similar to those found in Models:
``beforeFind``, ``afterFind``, ``beforeSave``, ``afterSave``, ``beforeDelete``,
``afterDelete`` and ``onError`` - see
``beforeFind``, ``afterFind``, ``beforeValidate``, ``afterValidate``,
``beforeSave``, ``afterSave``, ``beforeDelete``, ``afterDelete`` and
``onError`` - see
:doc:`/models/callback-methods`.

Your behaviors should be placed in ``app/Model/Behavior``. They are named in CamelCase and
Expand Down Expand Up @@ -260,9 +261,10 @@ and augment the parameters or splice in additional behavior.

All behavior callbacks are fired **before** the model/behavior callbacks are:

- ``beforeValidate``
- ``beforeFind``
- ``afterFind``
- ``beforeValidate``
- ``afterValidate``
- ``beforeSave``
- ``afterSave``
- ``beforeDelete``
Expand Down Expand Up @@ -296,39 +298,44 @@ model that the behavior method was invoked on.
Returning an array will augment the query parameters used for the
find operation.

.. php:method:: afterFind(Model $Model, mixed $results, boolean $primary)
.. php:method:: afterFind(Model $Model, mixed $results, boolean $primary = false)
You can use the afterFind to augment the results of a find. The
return value will be passed on as the results to either the next
behavior in the chain or the model's afterFind.

.. php:method:: beforeDelete(Model $Model, boolean $cascade = true)
.. php:method:: beforeValidate(Model $Model, array $options = array())
You can return false from a behavior's beforeDelete to abort the
delete. Return true to allow it continue.
You can use beforeValidate to modify a model's validate array or
handle any other pre-validation logic. Returning false from a
beforeValidate callback will abort the validation and cause it to
fail.

.. php:method:: afterDelete(Model $Model)
.. php:method:: afterValidate(Model $Model)
You can use afterDelete to perform clean up operations related to
your behavior.
You can use afterValidate to perform any data cleanup or preparation
if needed.

.. php:method:: beforeSave(Model $Model)
.. php:method:: beforeSave(Model $Model, array $options = array())
You can return false from a behavior's beforeSave to abort the
save. Return true to allow it continue.

.. php:method:: afterSave(Model $Model, boolean $created)
.. php:method:: afterSave(Model $Model, boolean $created, array $options = array())
You can use afterSave to perform clean up operations related to
your behavior. $created will be true when a record is created, and
false when a record is updated.

.. php:method:: beforeValidate(Model $Model)
.. php:method:: beforeDelete(Model $Model, boolean $cascade = true)
You can use beforeValidate to modify a model's validate array or
handle any other pre-validation logic. Returning false from a
beforeValidate callback will abort the validation and cause it to
fail.
You can return false from a behavior's beforeDelete to abort the
delete. Return true to allow it continue.

.. php:method:: afterDelete(Model $Model)
You can use afterDelete to perform clean up operations related to
your behavior.



Expand Down
20 changes: 15 additions & 5 deletions en/models/callback-methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ fired **before** model callbacks are.
beforeFind
==========

``beforeFind(array $queryData)``
``beforeFind(array $query)``

Called before any find-related operation. The ``$queryData`` passed
Called before any find-related operation. The ``$query`` 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
decision relating to the ``$query`` options), return *false*.
Otherwise, return the possibly modified ``$query``, or anything
you want to get passed to find and its counterparts.

You might use this callback to restrict find operations based on a
Expand Down Expand Up @@ -93,6 +93,14 @@ Use this callback to modify model data before it is validated, or
to modify validation rules if required. This function must also
return *true*, otherwise the current save() execution will abort.

afterValidate
==============

``afterValidate()``

Called after data has been checked for errors. Use this callback to perform
any data cleanup or preparation if needed.

beforeSave
==========

Expand Down Expand Up @@ -136,14 +144,16 @@ changed very easily. Use the code below in the appropriate model.
afterSave
=========

``afterSave(boolean $created)``
``afterSave(boolean $created, array $options = array())``

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 record was created
(rather than an update).

The ``$options`` array is the same one passed to ``Model::save()``.

beforeDelete
============

Expand Down

0 comments on commit c50da87

Please sign in to comment.