From 12daea109599f8d1a04243f36fe0eacb95230f7d Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 5 Nov 2014 14:00:21 +0100 Subject: [PATCH] Fixes `join` with related models which have "associative" conditions. Filtering for relations configured this way (associative conditions) was NOT working: ```php var $hasOne = array('Located' => array('conditions' => array( 'Located.model' => 'Employee', ))); ``` Filtering for relations configured this way was working: ```php var $hasOne = array('Located' => array('conditions' => array( 'Located.model = "Employee"', ))); ``` --- models/behaviors/filtered.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/models/behaviors/filtered.php b/models/behaviors/filtered.php index 9705727..69b8dcd 100644 --- a/models/behaviors/filtered.php +++ b/models/behaviors/filtered.php @@ -18,7 +18,7 @@ class FilteredBehavior extends ModelBehavior /** * Keeps current values after filter form post. * - * @var array + * @var array */ var $_filterValues = array(); @@ -171,7 +171,8 @@ function beforeFind(&$Model, $query) $customConditions = array($customConditions); } - $filterConditions = preg_replace(sprintf('#(?alias), $relatedModelAlias, $customConditions); + + $filterConditions = $this->_replaceArrayKeysAndValues(sprintf('#(?alias), $relatedModelAlias, $customConditions); $conditions = array_merge($conditions, $filterConditions); } @@ -275,4 +276,15 @@ function _setFilterValues(&$Model, $method, $values = array()) $this->_filterValues[$Model->alias] = array_merge($this->_filterValues[$Model->alias], $values); } + + function _replaceArrayKeysAndValues($pattern, $replacement, $input) + { + $keys = array_keys($input); + $values = array_values($input); + + $newKeys = preg_replace($pattern, $replacement, $keys); + $newValues = preg_replace($pattern, $replacement, $values); + + return array_combine($newKeys, $newValues); + } }