Skip to content

Commit

Permalink
Updates addWhere to allow passing of Expr objects
Browse files Browse the repository at this point in the history
  • Loading branch information
phpboyscout committed Nov 17, 2015
1 parent 1881705 commit 706a8a3
Showing 1 changed file with 56 additions and 50 deletions.
106 changes: 56 additions & 50 deletions src/ZucchiDoctrine/Query/QueryBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,60 +89,66 @@ protected function addWhere($qb, $where)
$par = $alias;
}
}

// process sets a little differently
if (!is_array($val)) {
$val = array($val);
}

if ($operator == 'regexp') {
$whereExp->add("REGEXP(" . $alias . '.' . $col . ",'" . $val[0] . "') = 1");

} else if ($operator == 'between') {
if (count($val) == 2) {
// $value should now be an array with 2 values
$expr= new Expr();
$from = (is_int($val[0])) ? $val[0] : "'" . $val[0] . "'";
$to = (is_int($val[1])) ? $val[1] : "'" . $val[1] . "'";

$stmt = $expr->between($alias . '.' . $col, $from, $to);
$whereExp->add($stmt);
}

} else if ($operator == 'is') {
$expr= new Expr();
$method = 'is' . ucfirst($val[0]);
if (method_exists($expr, $method)) {
$stmt = $expr->{$method}($alias . '.' . $col);
$whereExp->add($stmt);
if ($val instanceof Expr\Base) {
$whereExp->add($val);
} else {
// process sets a little differently
if (!is_array($val)) {
$val = array($val);
}
switch ($operator) {
case 'regexp':
$whereExp->add("REGEXP(" . $alias . '.' . $col . ",'" . $val[0] . "') = 1");
break;
case 'between':
if (count($val) == 2) {
// $value should now be an array with 2 values
$expr = new Expr();
$from = (is_int($val[0])) ? $val[0] : "'" . $val[0] . "'";
$to = (is_int($val[1])) ? $val[1] : "'" . $val[1] . "'";

} else {
// this holds the subquery for this field, each component being an OR
$subWhereExp = $qb->expr()->orX();

foreach ($val as $value) {
if ($value == null) {
$cmpValue = 'NULL';
} else {
$cmpValue = '?' . $i;

// wrap LIKE values
if ($operator == 'like') {
$value = '%' . trim($value, '%') . '%';
$stmt = $expr->between($alias . '.' . $col, $from, $to);
$whereExp->add($stmt);
}

// add the parameter value into the parameters stack
$params[$i] = $value;
$i++;
}

$comparison = new Expr\Comparison($alias . '.' . $col, $operator, $cmpValue);
$subWhereExp->add($comparison);
break;
case 'is':
$expr = new Expr();
$method = 'is' . ucfirst($val[0]);
if (method_exists($expr, $method)) {
$stmt = $expr->{$method}($alias . '.' . $col);
$whereExp->add($stmt);
}
break;
default:
// this holds the subquery for this field, each component being an OR
$subWhereExp = $qb->expr()->orX();

foreach ($val as $value) {
if ($value == null) {
$cmpValue = 'NULL';
} else {
$cmpValue = '?' . $i;

// wrap LIKE values
if ($operator == 'like') {
$value = '%' . trim($value, '%') . '%';
}

// add the parameter value into the parameters stack
$params[$i] = $value;
$i++;
}

$comparison = new Expr\Comparison($alias . '.' . $col, $operator, $cmpValue);
$subWhereExp->add($comparison);
}

// add in the subquery as an AND
$whereExp->add($subWhereExp);
break;

}

// add in the subquery as an AND
$whereExp->add($subWhereExp);
}
}

Expand Down Expand Up @@ -230,4 +236,4 @@ protected function addLimit($qb, $limit, $offset)
}
return $this;
}
}
}

0 comments on commit 706a8a3

Please sign in to comment.