2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Model behaviors base class.
|
|
|
|
*
|
|
|
|
* Adds methods and automagic functionality to Cake Models.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model
|
|
|
|
* @since CakePHP(tm) v 1.2.0.0
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Model behavior base class.
|
|
|
|
*
|
|
|
|
* Defines the Behavior interface, and contains common model interaction functionality.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class ModelBehavior extends Object {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains configuration settings for use with individual model objects. This
|
|
|
|
* is used because if multiple models use this Behavior, each will use the same
|
|
|
|
* object instance. Individual model settings should be stored as an
|
|
|
|
* associative array, keyed off of the model name.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
* @see Model::$alias
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $settings = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Allows the mapping of preg-compatible regular expressions to public or
|
|
|
|
* private methods in this class, where the array key is a /-delimited regular
|
|
|
|
* expression, and the value is a class method. Similar to the functionality of
|
|
|
|
* the findBy* / findAllBy* magic methods.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $mapMethods = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Setup this behavior with the specified configuration settings.
|
|
|
|
*
|
|
|
|
* @param object $model Model using this behavior
|
|
|
|
* @param array $config Configuration settings for $model
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function setup(&$model, $config = array()) { }
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Clean up any initialization this behavior has done on a model. Called when a behavior is dynamically
|
|
|
|
* detached from a model using Model::detach().
|
|
|
|
*
|
|
|
|
* @param object $model Model using this behavior
|
|
|
|
* @access public
|
|
|
|
* @see BehaviorCollection::detach()
|
|
|
|
*/
|
|
|
|
function cleanup(&$model) {
|
|
|
|
if (isset($this->settings[$model->alias])) {
|
|
|
|
unset($this->settings[$model->alias]);
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Before find callback
|
|
|
|
*
|
|
|
|
* @param object $model Model using this behavior
|
|
|
|
* @param array $queryData Data used to execute this query, i.e. conditions, order, etc.
|
2010-06-11 13:55:39 +00:00
|
|
|
* @return mixed False if the operation should abort. An array will replace the value of $query.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function beforeFind(&$model, $query) { }
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* After find callback. Can be used to modify any results returned by find and findAll.
|
|
|
|
*
|
|
|
|
* @param object $model Model using this behavior
|
|
|
|
* @param mixed $results The results of the find operation
|
|
|
|
* @param boolean $primary Whether this model is being queried directly (vs. being queried as an association)
|
2010-06-11 13:55:39 +00:00
|
|
|
* @return mixed An array value will replace the value of $results - any other value will be ignored.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function afterFind(&$model, $results, $primary) { }
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Before validate callback
|
|
|
|
*
|
|
|
|
* @param object $model Model using this behavior
|
2010-06-10 17:54:04 +00:00
|
|
|
* @return mixed False if the operation should abort. Any other result will continue.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function beforeValidate(&$model) { }
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Before save callback
|
|
|
|
*
|
|
|
|
* @param object $model Model using this behavior
|
2010-06-10 17:54:04 +00:00
|
|
|
* @return mixed False if the operation should abort. Any other result will continue.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function beforeSave(&$model) { }
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* After save callback
|
|
|
|
*
|
|
|
|
* @param object $model Model using this behavior
|
|
|
|
* @param boolean $created True if this save created a new record
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function afterSave(&$model, $created) { }
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Before delete callback
|
|
|
|
*
|
|
|
|
* @param object $model Model using this behavior
|
|
|
|
* @param boolean $cascade If true records that depend on this record will also be deleted
|
2010-06-10 17:54:04 +00:00
|
|
|
* @return mixed False if the operation should abort. Any other result will continue.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function beforeDelete(&$model, $cascade = true) { }
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* After delete callback
|
|
|
|
*
|
|
|
|
* @param object $model Model using this behavior
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function afterDelete(&$model) { }
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* DataSource error callback
|
|
|
|
*
|
|
|
|
* @param object $model Model using this behavior
|
|
|
|
* @param string $error Error generated in DataSource
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function onError(&$model, $error) { }
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* If $model's whitelist property is non-empty, $field will be added to it.
|
|
|
|
* Note: this method should *only* be used in beforeValidate or beforeSave to ensure
|
|
|
|
* that it only modifies the whitelist for the current save operation. Also make sure
|
|
|
|
* you explicitly set the value of the field which you are allowing.
|
|
|
|
*
|
|
|
|
* @param object $model Model using this behavior
|
|
|
|
* @param string $field Field to be added to $model's whitelist
|
|
|
|
* @access protected
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function _addToWhitelist(&$model, $field) {
|
|
|
|
if (is_array($field)) {
|
|
|
|
foreach ($field as $f) {
|
|
|
|
$this->_addToWhitelist($model, $f);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!empty($model->whitelist) && !in_array($field, $model->whitelist)) {
|
|
|
|
$model->whitelist[] = $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|