/** * For a give User name, run a partial search by * full name and retrieve user models. * */ public static function getUsersByPartialFullName($partialName, $pageSize) { assert('is_string($partialName)'); assert('is_int($pageSize)'); $personTableName = RedBeanModel::getTableName('Person'); $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('User'); $joinTablesAdapter->addFromTableAndGetAliasName($personTableName, "{$personTableName}_id"); $fullNameSql = DatabaseCompatibilityUtil::concat(array('person.firstname', '\' \'', 'person.lastname')); $where = " (person.firstname like lower('{$partialName}%') or " . " person.lastname like lower('{$partialName}%') or " . " {$fullNameSql} like lower('{$partialName}%')) "; return User::getSubset($joinTablesAdapter, null, $pageSize, $where, "person.firstname, person.lastname"); }
public static function getModelsByFullName($modelClassName, $fullName) { assert('is_string($modelClassName)'); assert('is_subclass_of($modelClassName, "Person") && $modelClassName != "Person" ||' . '$modelClassName == "User"'); assert('is_string($fullName)'); $personTableName = RedBeanModel::getTableName('Person'); $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter($modelClassName); $joinTablesAdapter->addFromTableAndGetAliasName($personTableName, "{$personTableName}_id"); $fullNameSql = DatabaseCompatibilityUtil::concat(array('person.firstname', '\' \'', 'person.lastname')); $where = "{$fullNameSql} = '{$fullName}'"; return $modelClassName::getSubset($joinTablesAdapter, null, null, $where); }
public function testConcat() { $res = DatabaseCompatibilityUtil::concat(array('column1', 'column2')); $this->assertEquals('concat(column1, column2)', $res); }
/** * Add a sql string to the where array base on the $operatorType, $value and $tableAliasAndColumnNames concated * together. How the sql string is built depends on if the value is a string or not. * @param string $operatorType * @param mixed $value * @param array $where * @param integer $whereKey * @param array $tableAliasAndColumnNames */ protected static function addWherePartByClauseInformationForConcatedAttributes($operatorType, $value, &$where, $whereKey, $tableAliasAndColumnNames) { assert('is_string($operatorType)'); assert('is_array($where)'); assert('is_int($whereKey)'); assert('is_array($tableAliasAndColumnNames) && count($tableAliasAndColumnNames) == 2'); $quote = DatabaseCompatibilityUtil::getQuote(); if (is_string($value) || is_array($value) && count($value) > 0 || $value !== null) { $first = ModelJoinBuilder::makeColumnNameWithTableAlias($tableAliasAndColumnNames[0][0], $tableAliasAndColumnNames[0][1]); $second = ModelJoinBuilder::makeColumnNameWithTableAlias($tableAliasAndColumnNames[1][0], $tableAliasAndColumnNames[1][1]); $concatedSqlPart = DatabaseCompatibilityUtil::concat(array($first, '\' \'', $second)); $where[$whereKey] = "({$concatedSqlPart} " . DatabaseCompatibilityUtil::getOperatorAndValueWherePart($operatorType, $value) . ")"; } }
protected static function getWherePartForPartialNameSearchByPartialName($partialName) { assert('is_string($partialName)'); $fullNameSql = DatabaseCompatibilityUtil::concat(array('person.firstname', '\' \'', 'person.lastname')); return " (person.firstname like '{$partialName}%' or " . " person.lastname like '{$partialName}%' or " . " {$fullNameSql} like '{$partialName}%') "; }
/** * Gets where part for full name search * @param string $fullName * @return string */ protected static function getWherePartForFullNameSearch($fullName) { assert('is_string($fullName)'); static::sanitizeSearchTerm($fullName); $fullNameSql = DatabaseCompatibilityUtil::concat(array('person.firstname', '\' \'', 'person.lastname')); return "{$fullNameSql} = '{$fullName}'"; }