silverstripe-framework/search/filters/PartialMatchFilter.php
torleifw 85de3866df FIX postgres can filter on non text fields
Filtering non text fields in postgres would cause the SQL to fail. This
casts the field to text before the LIKE filter.
2013-07-18 11:33:27 +12:00

36 lines
978 B
PHP

<?php
/**
* @package framework
* @subpackage search
*/
/**
* Matches textual content with a LIKE '%keyword%' construct.
*
* @package framework
* @subpackage search
*/
class PartialMatchFilter extends SearchFilter {
public function apply(DataQuery $query) {
$this->model = $query->applyRelation($this->relation);
$where = array();
$comparison = (DB::getConn() instanceof PostgreSQLDatabase) ? 'ILIKE' : 'LIKE';
$dbname = (DB::getConn() instanceof PostgreSQLDatabase) ? $this->getDbName().'::text' : $this->getDbName();
if(is_array($this->getValue())) {
foreach($this->getValue() as $value) {
$where[]= sprintf("%s %s '%%%s%%'", $dbname, $comparison, Convert::raw2sql($value));
}
} else {
$where[] = sprintf("%s %s '%%%s%%'", $dbname, $comparison, Convert::raw2sql($this->getValue()));
}
return $query->where(implode(' OR ', $where));
}
public function isEmpty() {
return $this->getValue() == null || $this->getValue() == '';
}
}