Last active
August 29, 2015 13:56
-
-
Save rudisimo/8964768 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function search(array $search = NULL, $sort = null, $order = "ASC", $limit = 10) { | |
$params = array(); | |
$values = array(); | |
// create query builder | |
$ns = 's'; | |
$qb = $this->createQueryBuilder($ns); | |
// build parameter bag | |
if ($search) { | |
foreach ($search as $column => $value) { | |
if (is_array($value)) { | |
$params[] = sprintf('%s.%s IN (:%s)', $ns, $column, $column); | |
} else { | |
$params[] = sprintf('%s.%s = :%s', $ns, $column, $column); | |
} | |
$values[$column] = $value; | |
} | |
} | |
// build where statement (only when necessary) | |
if (sizeof($params) > 0) { | |
$qb->where(join(' AND ', $params)); // you can change this to use expr() if you want | |
foreach ($values as $param => $value) { | |
$qb->setParameter($param, $value); | |
} | |
} | |
// add sort parameters | |
if (!is_null($sort)) { | |
$sortBy = sprintf('%s.%s', $ns, $sort); | |
$qb->orderBy($sortBy, $order); | |
} | |
// add query limit | |
$qb->setMaxResults($limit); | |
// build query | |
$query = $qb->getQuery(); | |
return $query->getResult(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Then you'd use it as
or
or