Skip to content

Instantly share code, notes, and snippets.

@iansltx
Created June 24, 2014 19:11
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 iansltx/6d0181c835e5afbf2965 to your computer and use it in GitHub Desktop.
Save iansltx/6d0181c835e5afbf2965 to your computer and use it in GitHub Desktop.
Query Building
<?php
class MyQuery {
protected function buildCollectionQuery($is_count_query = false) {
$qs = $is_count_query ? $this->baseCountQS : $this->baseCollectionQS;
$qsParams = [''];
if ($this->showInactive === false) {
$qs .= " && u.userStatus = 'active'";
}
if ($this->nameFilter !== null) {
$qs .= " && a.companyName LIKE ?";
$qsParams[] = '%'.$this->nameFilter.'%';
}
if (!$this->user->isAdmin()) {
$qs .= " && u.userManager = ?";
$qsParams[] = $this->user->getId();
}
if (count($this->sorts)) {
$qs .= ' ORDER BY ' . implode(', ', $this->sorts);
}
if (!$is_count_query) {
$qs .= ' LIMIT ' . $this->pageSize . ' OFFSET ' . $this->pageSize * ($this->page - 1);
}
return ['query' => $qs, 'params' => $qsParams];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment