From 09e64d075f65fed23085c5a3441de6c182f17f92 Mon Sep 17 00:00:00 2001 From: nate Date: Thu, 20 Sep 2007 15:16:25 +0000 Subject: [PATCH] Implementing beforeValidate() behavior callback (Ticket #2818), and allowing $actsAs to be specified in AppModel git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5676 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/behavior.php | 2 ++ cake/libs/model/model.php | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/cake/libs/model/behavior.php b/cake/libs/model/behavior.php index 30891621d..3d87ee805 100644 --- a/cake/libs/model/behavior.php +++ b/cake/libs/model/behavior.php @@ -64,6 +64,8 @@ class ModelBehavior extends Object { function afterFind(&$model, $results, $primary) { } + function beforeValidate(&$model) { } + function beforeSave(&$model) { } function afterSave(&$model, $created) { } diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 4ff520ef5..821db2947 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -32,6 +32,7 @@ * Included libs */ uses('class_registry', 'validation', 'overloadable', 'model' . DS . 'behavior', 'model' . DS . 'connection_manager', 'set'); + /** * Object-relational mapper. * @@ -380,6 +381,22 @@ class Model extends Overloadable { } } + if (is_subclass_of($this, 'AppModel')) { + $appVars = get_class_vars('AppModel'); + $actsAs = $appVars['actsAs']; + $merge = array('actsAs'); + + if ($this->actsAs !== null || $this->actsAs !== false) { + $merge[] = 'actsAs'; + } + + foreach ($merge as $var) { + if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) { + $this->{$var} = array_merge($this->{$var}, array_diff($appVars[$var], $this->{$var})); + } + } + } + if ($this->actsAs !== null && empty($this->behaviors)) { $callbacks = array('setup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave', 'beforeDelete', 'afterDelete', 'afterError'); $this->actsAs = Set::normalize($this->actsAs); @@ -1654,9 +1671,20 @@ class Model extends Overloadable { * Returns an array of invalid fields. * * @param array $data - * @return array Array of invalid fields or boolean case any error occurs + * @return array Array of invalid fields */ function invalidFields($data = array()) { + + if (!empty($this->behaviors)) { + $behaviors = array_keys($this->behaviors); + $ct = count($behaviors); + for ($i = 0; $i < $ct; $i++) { + if ($this->behaviors[$behaviors[$i]]->beforeValidate($this) === false) { + return $this->validationErrors; + } + } + } + if (!$this->beforeValidate()) { return $this->validationErrors; }