Making ModelBehavior beforeX callbacks default to returning true. Returning null or false from a Behavior::before method will abort the operation. It felt illogical to have null continue, but false stop.

This commit is contained in:
mark_story 2010-12-12 17:40:13 -05:00
parent 48879f8264
commit 3c69d9b138
2 changed files with 19 additions and 11 deletions

View file

@ -1322,7 +1322,7 @@ class Model extends Object {
if ($options['callbacks'] === true || $options['callbacks'] === 'before') { if ($options['callbacks'] === true || $options['callbacks'] === 'before') {
$result = $this->Behaviors->trigger('beforeSave', array(&$this, $options), array( $result = $this->Behaviors->trigger('beforeSave', array(&$this, $options), array(
'break' => true, 'breakOn' => false 'break' => true, 'breakOn' => array(false, null)
)); ));
if (!$result || !$this->beforeSave($options)) { if (!$result || !$this->beforeSave($options)) {
$this->whitelist = $_whitelist; $this->whitelist = $_whitelist;
@ -1854,7 +1854,7 @@ class Model extends Object {
$filters = $this->Behaviors->trigger( $filters = $this->Behaviors->trigger(
'beforeDelete', 'beforeDelete',
array(&$this, $cascade), array(&$this, $cascade),
array('break' => true, 'breakOn' => false) array('break' => true, 'breakOn' => array(false, null))
); );
if (!$filters || !$this->exists()) { if (!$filters || !$this->exists()) {
return false; return false;
@ -1876,7 +1876,7 @@ class Model extends Object {
if (!empty($this->belongsTo)) { if (!empty($this->belongsTo)) {
$this->updateCounterCache($keys[$this->alias]); $this->updateCounterCache($keys[$this->alias]);
} }
$this->Behaviors->trigger($this, 'afterDelete'); $this->Behaviors->trigger('afterDelete', array(&$this));
$this->afterDelete(); $this->afterDelete();
$this->_clearCache(); $this->_clearCache();
$this->id = false; $this->id = false;
@ -2142,8 +2142,8 @@ class Model extends Object {
if ($query['callbacks'] === true || $query['callbacks'] === 'before') { if ($query['callbacks'] === true || $query['callbacks'] === 'before') {
$return = $this->Behaviors->trigger( $return = $this->Behaviors->trigger(
'beforeFind', 'beforeFind',
array(&$this, $query), array(&$this, $query),
array('break' => true, 'breakOn' => false, 'modParams' => 1) array('break' => true, 'breakOn' => array(false, null), 'modParams' => 1)
); );
$query = (is_array($return)) ? $return : $query; $query = (is_array($return)) ? $return : $query;

View file

@ -80,7 +80,7 @@ class ModelBehavior extends Object {
* *
* @param object $model Model using this behavior * @param object $model Model using this behavior
* @param array $queryData Data used to execute this query, i.e. conditions, order, etc. * @param array $queryData Data used to execute this query, i.e. conditions, order, etc.
* @return mixed False if the operation should abort. An array will replace the value of $query. * @return mixed False or null will abort the operation. You should array will replace the value of $query.
* @access public * @access public
*/ */
public function beforeFind($model, $query) { } public function beforeFind($model, $query) { }
@ -100,10 +100,12 @@ class ModelBehavior extends Object {
* Before validate callback * Before validate callback
* *
* @param object $model Model using this behavior * @param object $model Model using this behavior
* @return mixed False if the operation should abort. Any other result will continue. * @return mixed False or null will abort the operation. Any other result will continue.
* @access public * @access public
*/ */
public function beforeValidate($model) { } public function beforeValidate($model) {
return true;
}
/** /**
* Before save callback * Before save callback
@ -112,7 +114,9 @@ class ModelBehavior extends Object {
* @return mixed False if the operation should abort. Any other result will continue. * @return mixed False if the operation should abort. Any other result will continue.
* @access public * @access public
*/ */
public function beforeSave($model) { } public function beforeSave($model) {
return true;
}
/** /**
* After save callback * After save callback
@ -120,7 +124,9 @@ class ModelBehavior extends Object {
* @param object $model Model using this behavior * @param object $model Model using this behavior
* @param boolean $created True if this save created a new record * @param boolean $created True if this save created a new record
*/ */
public function afterSave($model, $created) { } public function afterSave($model, $created) {
return true;
}
/** /**
* Before delete callback * Before delete callback
@ -130,7 +136,9 @@ class ModelBehavior extends Object {
* @return mixed False if the operation should abort. Any other result will continue. * @return mixed False if the operation should abort. Any other result will continue.
* @access public * @access public
*/ */
public function beforeDelete($model, $cascade = true) { } public function beforeDelete($model, $cascade = true) {
return true;
}
/** /**
* After delete callback * After delete callback