lithium\data\source\Database::_operator()
Handles conversion of SQL operator keys to SQL statements.
Parameters
-
string
$key
Key in a conditions array. Usually a field name.
-
mixed
$value
An SQL operator or comparison value.
-
array
$schema
An array defining the schema of the field used in the criteria.
-
array
$options
Returns
stringReturns an SQL string representing part of a WHERE
clause of a query.
Source
protected function _operator($key, $value, array $schema = [], array $options = []) {
$defaults = ['boolean' => 'AND'];
$options += $defaults;
$op = strtoupper(key($value));
$value = current($value);
$config = $this->_operators[$op];
$key = $this->name($key);
$values = [];
if (!is_object($value)) {
if ($value === null) {
$value = [null];
}
foreach ((array) $value as $val) {
$values[] = $this->value($val, $schema);
}
} elseif (isset($value->scalar)) {
return "{$key} {$op} {$value->scalar}";
}
switch (true) {
case (isset($config['format'])):
return $key . ' ' . Text::insert($config['format'], $values);
case (is_object($value) && isset($config['multiple'])):
$op = $config['multiple'];
$value = trim(rtrim($this->renderCommand($value), ';'));
return "{$key} {$op} ({$value})";
case (count($values) > 1 && isset($config['multiple'])):
$op = $config['multiple'];
$values = join(', ', $values);
return "{$key} {$op} ({$values})";
case (count($values) > 1):
return join(" {$options['boolean']} ", array_map(
function($v) use ($key, $op) { return "{$key} {$op} {$v}"; }, $values
));
}
return "{$key} {$op} {$values[0]}";
}