Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Search Function Rules
/**
* Search methods MUST return an array with "rowCount" and "rowSet".
*
* Search methods build queries with provided params (filters) and
* options (display filters - page, items per page, etc. Search
* methods can then use the buildAndReturnMySQL method from the
* Database/Abstract, or they can return their own result set. The
* only important thing is that they always return a single array
* which contains "rowCount" and "rowSet" - this is very important for
* pagination.
*
* Example fetchContacts methods which fetches contacts with some
* filters and display options:
*/
public function fetchContacts($aParams, $aOptions = array())
{
$sQuery = ' SELECT %s FROM ' . $this->sContacts . ' `contacts` WHERE 1 ';
$aBind = array();
if (isset($aParams['q']) && !empty($aParams['q'])) {
if (is_numeric($aParams['q'])) {
$sQuery .= ' AND `id` = :q ';
$aBind['q'] = (int)$aParams['q'];
} else {
$sQuery .= ' AND `email` LIKE :q';
$aBind['q'] = '%' . $aParams['q'] . '%';
}
}
if (isset($aParams['email'])) {
$sQuery .= ' AND `email` = :email';
$aBind['email'] = '%' . $aParams['email'] . '%';
}
if (isset($aParams['accountId']) && (int)$aParams['accountId'] > 0) {
$sQuery .= ' = :accountId';
$aBind['accountId'] = $aParams['accountId'];
}
if (isset($aParams['hasAccount'])) {
if ($aParams['hasAccount'] === true) {
$sQuery .= ' AND accountId IS NOT NULL ';
} else {
$sQuery .= ' AND accountId IS NULL ';
}
}
if (isset($aParams['deleted']) && $aParams['deleted'] !== '') {
$sQuery .= ' AND `deleted` = :deleted ';
$aBind['deleted'] = $aParams['deleted'];
}
if (isset($aParams['activated']) && $aParams['activated'] !== '') {
$sQuery .= ' AND `activated` = :activated ';
$aBind['activated'] = $aParams['activated'];
}
if (isset($aParams['validity']) && !empty($aParams['validity'])) {
$sQuery .= ' AND valid = :valid ';
$aBind['valid'] = $aParams['validity'];
}
// See method in Abstract
return $this->buildAndReturnMySQL($sQuery, $aBind, $aOptions);
}
/**
* This then calls the auto-build and auto-return method in the parent
* abstract class: buildAndReturnMySQL
*
* If this method is not expressive enough for you, just build the entire
* method in your model (Database.php file probably) as you usually would.
* Just make sure it returns the correct format of the array.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment