类 yii\db\conditions\LikeConditionBuilder
类 LikeConditionBuilder 构建 yii\db\conditions\LikeCondition 对象
公共方法
方法 | 描述 | 定义于 |
---|---|---|
__construct() | ExpressionBuilderTrait 构造函数。 | yii\db\ExpressionBuilderTrait |
build() | 此方法从 $expression 构建原始 SQL,不会额外转义或引用。 | yii\db\conditions\LikeConditionBuilder |
属性详情
用于转义 LIKE 条件中特殊字符的字符。默认情况下,假定为 \
。
LIKE 条件中字符与其替换的映射。默认情况下,配置为使用 \
转义 %
、_
和 \
。
方法详情
定义于: yii\db\ExpressionBuilderTrait::__construct()
ExpressionBuilderTrait 构造函数。
public void __construct ( yii\db\QueryBuilder $queryBuilder ) | ||
$queryBuilder | yii\db\QueryBuilder |
public function __construct(QueryBuilder $queryBuilder)
{
$this->queryBuilder = $queryBuilder;
}
此方法从 $expression 构建原始 SQL,不会额外转义或引用。
public string build ( yii\db\ExpressionInterface $expression, array &$params = [] ) | ||
$expression | yii\db\ExpressionInterface|yii\db\conditions\LikeCondition |
要构建的表达式。 |
$params | array |
绑定参数。 |
返回值 | string |
不会额外转义或引用的原始 SQL。 |
---|
public function build(ExpressionInterface $expression, array &$params = [])
{
$operator = strtoupper($expression->getOperator());
$column = $expression->getColumn();
$values = $expression->getValue();
$escape = $expression->getEscapingReplacements();
if ($escape === null || $escape === []) {
$escape = $this->escapingReplacements;
}
list($andor, $not, $operator) = $this->parseOperator($operator);
if (!is_array($values)) {
$values = [$values];
}
if (empty($values)) {
return $not ? '' : '0=1';
}
if ($column instanceof ExpressionInterface) {
$column = $this->queryBuilder->buildExpression($column, $params);
} elseif (is_string($column) && strpos($column, '(') === false) {
$column = $this->queryBuilder->db->quoteColumnName($column);
}
$escapeSql = $this->getEscapeSql();
$parts = [];
foreach ($values as $value) {
if ($value instanceof ExpressionInterface) {
$phName = $this->queryBuilder->buildExpression($value, $params);
} else {
$phName = $this->queryBuilder->bindParam(empty($escape) ? $value : ('%' . strtr((string)$value, $escape) . '%'), $params);
}
$parts[] = "{$column} {$operator} {$phName}{$escapeSql}";
}
return implode($andor, $parts);
}
protected array parseOperator ( $operator ) | ||
$operator | string |
protected function parseOperator($operator)
{
if (!preg_match('/^(AND |OR |)(((NOT |))I?LIKE)/', $operator, $matches)) {
throw new InvalidArgumentException("Invalid operator '$operator'.");
}
$andor = ' ' . (!empty($matches[1]) ? $matches[1] : 'AND ');
$not = !empty($matches[3]);
$operator = $matches[2];
return [$andor, $not, $operator];
}
注册 或 登录 以发表评论。