Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
Add support for ilike to SearchableBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
josegonzalez committed Jun 24, 2014
1 parent b122dc7 commit e91e48a
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions Model/Behavior/SearchableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SearchableBehavior extends ModelBehavior {
* - wildcardAny: the character used instead of % (% is a normal character then)
* - wildcardOne: the character used instead of _ (_ is a normal character then)
* - like: auto add % wildcard to beginning, end or both (both false => user can enter wildcards himself)
* - ilike: auto add % wildcard to beginning, end or both (both false => user can enter wildcards himself)
* - connectorAnd: the character between search terms to specify an "and" relationship (binds stronger than or, similar to * and + in math)
* - connectorOr: the character between search terms to specify an "or" relationship
*
Expand All @@ -30,6 +31,7 @@ class SearchableBehavior extends ModelBehavior {
'wildcardAny' => '*', //on windows/unix/mac/google/... thats the default one
'wildcardOne' => '?', //on windows/unix/mac thats the default one
'like' => array('before' => true, 'after' => true),
'ilike' => array('before' => true, 'after' => true),
'connectorAnd' => null,
'connectorOr' => null,
);
Expand Down Expand Up @@ -95,7 +97,9 @@ public function parseCriteria(Model $Model, $data) {
}

if (in_array($field['type'], array('like'))) {
$this->_addCondLike($Model, $conditions, $data, $field);
$this->_addCondLike($Model, $conditions, $data, $field, 'LIKE');
if (in_array($field['type'], array('ilike'))) {
$this->_addCondLike($Model, $conditions, $data, $field, 'ILIKE');
} elseif (in_array($field['type'], array('value', 'lookup'))) {
$this->_addCondValue($Model, $conditions, $data, $field);
} elseif ($field['type'] === 'expression') {
Expand Down Expand Up @@ -181,6 +185,28 @@ public function unbindAllModels(Model $Model, $reset = false) {
$Model->unbindModel($unbind, $reset);
}

/**
* For custom queries inside the model
* example "makePhoneCondition": $cond = array('OR' => array_merge($this->condIlike('cell_number', $filter), $this->condIlike('landline_number', $filter, array('before' => false))));
*
* @param Model $Model
* @param $name
* @param $data
* @param array $field
* @return array of conditions
*/
public function condIlike(Model $Model, $name, $data, $field = array()) {
$conditions = array();
$field['name'] = $name;
if (!is_array($data)) {
$data = array($name => $data);
}
if (!isset($field['field'])) {
$field['field'] = $field['name'];
}
return $this->_addCondLike($Model, $conditions, $data, $field, 'ILIKE');
}

/**
* For custom queries inside the model
* example "makePhoneCondition": $cond = array('OR' => array_merge($this->condLike('cell_number', $filter), $this->condLike('landline_number', $filter, array('before' => false))));
Expand All @@ -200,7 +226,7 @@ public function condLike(Model $Model, $name, $data, $field = array()) {
if (!isset($field['field'])) {
$field['field'] = $field['name'];
}
return $this->_addCondLike($Model, $conditions, $data, $field);
return $this->_addCondLike($Model, $conditions, $data, $field, 'LIKE');
}

/**
Expand Down Expand Up @@ -237,7 +263,7 @@ public function formatLike(Model $Model, $data, $options = array()) {
}

/**
* Return the current chars for querying LIKE statements on this model
* Return the current chars for querying LIKE/ILIKE statements on this model
*
* @param Model $Model Reference to the model
* @param array $options
Expand All @@ -257,11 +283,12 @@ public function getWildcards(Model $Model, $options = array()) {
* @param array $field Field definition information
* @return array Conditions
*/
protected function _addCondLike(Model $Model, &$conditions, $data, $field) {
if (!is_array($this->settings[$Model->alias]['like'])) {
$this->settings[$Model->alias]['like'] = array('before' => $this->settings[$Model->alias]['like'], 'after' => $this->settings[$Model->alias]['like']);
protected function _addCondLike(Model $Model, &$conditions, $data, $field, $likeMethod = 'LIKE') {
$settingName = strtolower($likeMethod);
if (!is_array($this->settings[$Model->alias][$settingName])) {
$this->settings[$Model->alias][$settingName] = array('before' => $this->settings[$Model->alias][$settingName], 'after' => $this->settings[$Model->alias][$settingName]);
}
$field = array_merge($this->settings[$Model->alias]['like'], $field);
$field = array_merge($this->settings[$Model->alias][$settingName], $field);
if (empty($data[$field['name']])) {
return $conditions;
}
Expand Down Expand Up @@ -300,9 +327,9 @@ protected function _addCondLike(Model $Model, &$conditions, $data, $field) {
}

if (!empty($field['connectorAnd']) || !empty($field['connectorOr'])) {
$cond[] = $this->_connectedLike($value, $field, $fieldName);
$cond[] = $this->_connectedLike($value, $field, $fieldName, $likeMethod);
} else {
$cond[$fieldName . " LIKE"] = $field['before'] . $value . $field['after'];
$cond[$fieldName . " " . $likeMethod] = $field['before'] . $value . $field['after'];
}
}
if (count($cond) > 1) {
Expand All @@ -326,14 +353,14 @@ protected function _addCondLike(Model $Model, &$conditions, $data, $field) {
* @param string $fieldName
* @return array Conditions
*/
protected function _connectedLike($value, $field, $fieldName) {
protected function _connectedLike($value, $field, $fieldName, $likeMethod = 'LIKE') {
$or = array();
$orValues = String::tokenize($value, $field['connectorOr']);
foreach ($orValues as $orValue) {
$andValues = String::tokenize($orValue, $field['connectorAnd']);
$and = array();
foreach ($andValues as $andValue) {
$and[] = array($fieldName . " LIKE" => $field['before'] . $andValue . $field['after']);
$and[] = array($fieldName . " " . $likeMethod => $field['before'] . $andValue . $field['after']);
}

$or[] = array('AND' => $and);
Expand Down

0 comments on commit e91e48a

Please sign in to comment.