03.29
php
saved
polerin
Note
Generic multi-field search Stick this in your AppModel. Original code by me, heavily refactored and added too by the excellent Mr. TommyO. This is a bug fixed version. Let me know if there's anything useful or wrong that you'd like added/fixed.
Generic multi-field search Stick this in your AppModel. Original code by me, heavily refactored and added too by the excellent Mr. TommyO. This is a bug fixed version. Let me know if there's anything useful or wrong that you'd like added/fixed.
- /**
- * Search in the model for $term in $fields. All members of $terms are exploded()'ed on ' '
- *
- * @return array of matches.
- * @param {Array, String} $terms String or strings to search for
- * @param {Array, String} $searchFields Field names
- * @param {Array} $params Params to pass to find [Optional]
- */
- //make sure everything we need is in array form
- }
- }
- foreach ($terms as $term) {
- //merge it in
- }
- $type = 'OR';
- $not = ($term{0} == '-');
- // force/ignore word by prepending +/-
- $type = 'AND';
- }
- // handle protected strings
- if ($term{0} == '"') {
- }
- }
- if ($term != '') {
- foreach ($searchFields as $field) {
- $not = ($not) ? "NOT " : "";
- $conditions[$type][] = "{$field} {$not}LIKE '%{$term}%'";
- }
- }
- }
- //set our params
- $fields = $order = $page = $limit = null;
- //aaaaaaaand return!
- return $this->findAll($conditions, $fields, $order, $page, $limit);
- }
Parsed in 0.093 seconds, using GeSHi 1.0.7.14