Skip to content

Instantly share code, notes, and snippets.

@norbe
Created January 10, 2017 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norbe/9a93330e87af257f8239b8e8cc229381 to your computer and use it in GitHub Desktop.
Save norbe/9a93330e87af257f8239b8e8cc229381 to your computer and use it in GitHub Desktop.
How to create IN condition on more columns in nette database
/**
* Create where query parts needed for filtering rows by more than 1 value per row.
*
* Example:
* For [[value: 1, type: 2],[value: 2, type: 3]]
* creates (value = 1 AND type = 2) OR (value = 2 AND type = 3)
*
* @author Karel Hák <karel.hak@fregis.cz>
*/
class WhereMultiInBuilder extends \Nette\Object {
private $conditionRows;
private $condition;
private $parameters;
public function __construct($conditionRows = array()) {
$this->conditionRows = $conditionRows;
}
private function build() {
if(is_null($this->condition)) {
$conditions = array();
$this->parameters = array();
foreach($this->conditionRows as $item) {
$condition = array();
foreach($item as $key => $value) {
$condition[] = "$key ?";
$this->parameters[] = $value;
}
$conditions[] = '(' . implode(' AND ', $condition) . ')';
}
$this->condition = '('
. (count($conditions) ? implode(' OR ', $conditions) : '0')
. ')';
}
}
public function getCondition() {
$this->build();
return $this->condition;
}
public function getParameters() {
$this->build();
return $this->parameters;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment