Skip to content

Instantly share code, notes, and snippets.

@jackdpeterson
Created March 2, 2018 20:46
Show Gist options
  • Save jackdpeterson/11f132d0f4a45f96816dcde36f824a02 to your computer and use it in GitHub Desktop.
Save jackdpeterson/11f132d0f4a45f96816dcde36f824a02 to your computer and use it in GitHub Desktop.
When you are using the where In SQL-like search and provide an empty array for the in side of things ... DQL will barf and die with a syntax error. The fix is to check the count of items in the parameter being passed in and create a single entry with a non-possible value to filter down. /** * Creates an IN() expression with the given arguments. …
/**
* Creates an IN() expression with the given arguments.
*
* @param string $x Field in string format to be restricted by IN() function.
* @param mixed $y Argument to be used in IN() function.
*
* @return Expr\Func
*/
public function in($x, $y)
{
if (is_array($y)) {
foreach ($y as &$literal) {
if ( ! ($literal instanceof Expr\Literal)) {
$literal = $this->_quoteLiteral($literal);
}
}
}
return new Expr\Func($x . ' IN', (array) $y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment