09/05/2012 2:59am

PHP | Problem - Search Filter


<?php

/**
* @package sapphire
* @subpackage search
*/

/**
* Checks if a value is in a given set.
* SQL syntax used: Column FIND_IN_SET('val1','val2')
*
* @todo Add negation (NOT IN)6
* @package sapphire
* @subpackage search
*/
class ExactMatchMultiEnumFilter extends SearchFilter {

public function apply(SQLQuery $query) {
$query = $this->applyRelation($query);

// hack
// PREVIOUS $values = explode(',',$this->getValue());
$values = array();
if (is_string($this->getValue())) {
$values = explode(',',$this->getValue());
}
else {
foreach($this->getValue() as $v) {
$values[] = $v;
}
}


if(! $values) return false;
for($i = 0; $i < count($values); $i++) {
if(! is_numeric($values[$i])) {
// @todo Fix string replacement to only replace leading and tailing quotes
$values[$i] = str_replace("'", '', $values[$i]);
$values[$i] = Convert::raw2sql($values[$i]);
}
}
$SQL_valueStr = "'" . implode("','", $values) . "'";

return $query->where(sprintf(
"%s LIKE '%%%s%%'",
$this->getDbName(),
$SQL_valueStr
));
}

public function isEmpty() {
return $this->getValue() == null || $this->getValue() == '';
}
}
?>


2 Comments 2 Solutions

09/05/2012 3:14am

PHP | Solution - Anonymous


/**
* @package sapphire
* @subpackage search
*/

/**
* Checks if a value is in a given set.
* SQL syntax used: Column FIND_IN_SET('val1','val2')
*
* @todo Add negation (NOT IN)6
* @package sapphire
* @subpackage search
*/
class ExactMatchMultiEnumFilter extends SearchFilter {

public function apply(SQLQuery $query) {
$query = $this->applyRelation($query);

// hack
// PREVIOUS $values = explode(',',$this->getValue());
$values = array();
if (is_string($this->getValue())) {
$values = explode(',',$this->getValue());
}
else {
foreach($this->getValue() as $v) {
$values[] = $v;
}
}


if(! $values) return false;
for($i = 0; $i < count($values); $i++) {
// @todo Fix string replacement to only replace leading and tailing quotes
$values[$i] = str_replace("'", '', $values[$i]);
$values[$i] = sprintf("FIND_IN_SET('%s', %s)", Convert::raw2sql($values[$i]), $this->getDbName());
}
return $query->where('(' . implode(' OR ', $values) . ')');
}

public function isEmpty() {
return $this->getValue() == null || $this->getValue() == '';
}
}

09/05/2012 3:06am

PHP | Solution - Anonymous


/**
* @package sapphire
* @subpackage search
*/

/**
* Checks if a value is in a given set.
* SQL syntax used: Column FIND_IN_SET('val1','val2')
*
* @todo Add negation (NOT IN)6
* @package sapphire
* @subpackage search
*/
class ExactMatchMultiEnumFilter extends SearchFilter {

public function apply(SQLQuery $query) {
$query = $this->applyRelation($query);

// hack
// PREVIOUS $values = explode(',',$this->getValue());
$values = array();
if (is_string($this->getValue())) {
$values = explode(',',$this->getValue());
}
else {
foreach($this->getValue() as $v) {
$values[] = $v;
}
}


if(! $values) return false;
for($i = 0; $i < count($values); $i++) {
if(! is_numeric($values[$i])) {
// @todo Fix string replacement to only replace leading and tailing quotes
$val = str_replace("'", '', $values[$i]);
$query = $query->where(sprintf("FIND_IN_SET('%s', %s)", Convert::raw2sql($val), $this->getDbName()));
}
}

return $query;
}

public function isEmpty() {
return $this->getValue() == null || $this->getValue() == '';
}
}

Post Comment