Binding additional Behavior callbacks to Model

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4108 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-12-19 19:26:02 +00:00
parent eeec18d562
commit 982381dc74
2 changed files with 88 additions and 11 deletions

View file

@ -29,19 +29,42 @@
*/
class ModelBehavior extends Object {
/**
* 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
*/
var $settings = array();
/**
* 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
*/
var $mapMethods = array();
function setup(&$model, $config = array()) { }
function beforeFind(&$model, &$query) { }
function beforeFind(&$model, $query) { }
function afterFind(&$model, &$results, $primary) { }
function afterFind(&$model, $results, $primary) { }
function beforeSave(&$model) { }
function afterSave(&$model) { }
function beforeDelete(&$model) { }
function afterDelete(&$model) { }
function onError(&$model, &$error) { }
function onError(&$model, $error) { }
}
?>

View file

@ -465,6 +465,7 @@ class Model extends Overloadable {
}
}
$return = $db->query($method, $params, $this);
if (!PHP5) {
if (isset($this->__backAssociation)) {
$this->__resetAssociations();
@ -809,6 +810,15 @@ class Model extends Overloadable {
* @return boolean
*/
function hasField($name) {
if (is_array($name)) {
foreach ($name as $n) {
if ($this->hasField($n)) {
return $n;
}
}
return false;
}
if (empty($this->_tableInfo)) {
$this->loadInfo();
}
@ -939,6 +949,16 @@ class Model extends Overloadable {
return false;
}
if (!empty($this->behaviors)) {
$behaviors = array_keys($this->behaviors);
$ct = count($behaviors);
for ($i = 0; $i < $ct; $i++) {
if ($this->behaviors[$behaviors[$i]]->beforeSave($this) === false) {
return false;
}
}
}
if (!$this->beforeSave()) {
return false;
}
@ -1030,6 +1050,13 @@ class Model extends Overloadable {
$this->__saveMulti($joined, $this->id);
}
if (!empty($this->behaviors)) {
$behaviors = array_keys($this->behaviors);
$ct = count($behaviors);
for ($i = 0; $i < $ct; $i++) {
$this->behaviors[$behaviors[$i]]->afterSave($this);
}
}
$this->afterSave();
$this->data = false;
$this->_clearCache();
@ -1133,10 +1160,27 @@ class Model extends Overloadable {
if ($this->exists() && $this->beforeDelete()) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
if ($this->id && $db->delete($this)) {
if (!empty($this->behaviors)) {
$behaviors = array_keys($this->behaviors);
$ct = count($behaviors);
for ($i = 0; $i < $ct; $i++) {
if ($this->behaviors[$behaviors[$i]]->beforeDelete($this) === false) {
return false;
}
}
}
if ($db->delete($this)) {
$this->_deleteMulti($this->id);
$this->_deleteHasMany($this->id, $cascade);
$this->_deleteHasOne($this->id, $cascade);
if (!empty($this->behaviors)) {
for ($i = 0; $i < $ct; $i++) {
if ($this->behaviors[$behaviors[$i]]->afterDelete($this) === false) {
return false;
}
}
}
$this->afterDelete();
$this->_clearCache();
$this->id = false;
@ -1323,14 +1367,22 @@ class Model extends Overloadable {
);
if (!empty($this->behaviors)) {
$b = array_keys($this->behaviors);
$c = count($b);
for ($i = 0; $i < $c; $i++) {
$this->behaviors[$b[$i]]->beforeFind($this, $queryData);
$behaviors = array_keys($this->behaviors);
$ct = count($behaviors);
for ($i = 0; $i < $ct; $i++) {
$ret = $this->behaviors[$behaviors[$i]]->beforeFind($this, $queryData);
if (is_array($ret)) {
$queryData = $ret;
} elseif ($ret === false) {
return null;
}
}
}
if (!$this->beforeFind($queryData)) {
$ret = $this->beforeFind($queryData);
if (is_array($ret)) {
$queryData = $ret;
} elseif ($ret === false) {
return null;
}
@ -1340,10 +1392,12 @@ class Model extends Overloadable {
$b = array_keys($this->behaviors);
$c = count($b);
for ($i = 0; $i < $c; $i++) {
$this->behaviors[$b[$i]]->afterFind($this, $results, true);
$ret = $this->behaviors[$b[$i]]->afterFind($this, $results, true);
if (is_array($ret)) {
$results = $ret;
}
}
}
$return = $this->afterFind($results, true);
if (isset($this->__backAssociation)) {