From 6ae22129388587d1954b3e9401a93f127709596e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Mon, 20 Jan 2014 11:44:42 +0100 Subject: [PATCH] Fixing the filterArgs initialization --- Model/Behavior/SearchableBehavior.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Model/Behavior/SearchableBehavior.php b/Model/Behavior/SearchableBehavior.php index 6dd9c16..d2d36bb 100755 --- a/Model/Behavior/SearchableBehavior.php +++ b/Model/Behavior/SearchableBehavior.php @@ -44,8 +44,22 @@ class SearchableBehavior extends ModelBehavior { public function setup(Model $Model, $config = array()) { $this->_defaults = array_merge($this->_defaults, (array)Configure::read('Search.Searchable')); $this->settings[$Model->alias] = array_merge($this->_defaults, $config); + } + +/** + * Prepares the filter args based on the model information and calls + * Model::getFilterArgs if present to set up the filterArgs with proper model + * aliases. + * + * @param Model $Model + * @return boolean|array + */ + public function setupFilterArgs(Model $Model) { + if (method_exists($Model, 'getFilterArgs')) { + $Model->getFilterArgs(); + } if (empty($Model->filterArgs)) { - return; + return false; } foreach ($Model->filterArgs as $key => $val) { if (!isset($val['name'])) { @@ -58,6 +72,7 @@ public function setup(Model $Model, $config = array()) { $Model->filterArgs[$key]['type'] = 'value'; } } + return $Model->filterArgs; } /** @@ -70,7 +85,9 @@ public function setup(Model $Model, $config = array()) { * @return array Array of conditions that express the conditions needed for the search */ public function parseCriteria(Model $Model, $data) { + $this->setupFilterArgs($Model); $conditions = array(); + foreach ($Model->filterArgs as $field) { // If this field was not passed and a default value exists, use that instead. if (!array_key_exists($field['name'], $data) && array_key_exists('defaultValue', $field)) { @@ -120,6 +137,8 @@ public function validateSearch(Model $Model, $data = null) { * @return array, filtered args */ public function passedArgs(Model $Model, $vars) { + $this->setupFilterArgs($Model); + $result = array(); foreach ($vars as $var => $val) { if (in_array($var, Set::extract($Model->filterArgs, '{n}.name'))) {