\Cake\Database\ExpressionQueryExpression

Represents a SQL Query expression. Internally it stores a tree of expressions that can be compiled by converting this object to string and will contain a correctly parenthesized and nested expression.

Summary

Methods
Properties
Constants
setTypeMap()
getTypeMap()
typeMap()
setDefaultTypes()
getDefaultTypes()
defaultTypes()
__construct()
setConjunction()
getConjunction()
tieWith()
type()
add()
eq()
notEq()
gt()
lt()
gte()
lte()
isNull()
isNotNull()
like()
notLike()
in()
addCase()
notIn()
exists()
notExists()
between()
and_()
or_()
not()
count()
equalFields()
sql()
traverse()
iterateParts()
__call()
isCallable()
hasNestedExpression()
__clone()
No public properties found
No constants found
_addConditions()
_parseCondition()
_calculateType()
$_typeMap
$_conjunction
$_conditions
N/A
No private methods found
No private properties found
N/A

Properties

$_conjunction

$_conjunction : string

String to be used for joining each of the internal expressions this object internally stores for example "AND", "OR", etc.

Type

string

$_conditions

$_conditions : array

A list of strings or other expression objects that represent the "branches" of the expression tree. For example one key of the array might look like "sum > :value"

Type

array

Methods

setTypeMap()

setTypeMap(array|\Cake\Database\TypeMap  $typeMap) : $this

Creates a new TypeMap if $typeMap is an array, otherwise exchanges it for the given one.

Parameters

array|\Cake\Database\TypeMap $typeMap

Creates a TypeMap if array, otherwise sets the given TypeMap

Returns

$this

getTypeMap()

getTypeMap() : \Cake\Database\TypeMap

Returns the existing type map.

Returns

\Cake\Database\TypeMap

typeMap()

typeMap(array|\Cake\Database\TypeMap|null  $typeMap = null) : $this|\Cake\Database\TypeMap

Creates a new TypeMap if $typeMap is an array, otherwise returns the existing type map or exchanges it for the given one.

Parameters

array|\Cake\Database\TypeMap|null $typeMap

Creates a TypeMap if array, otherwise sets the given TypeMap

Returns

$this|\Cake\Database\TypeMap

setDefaultTypes()

setDefaultTypes(array  $types) : $this

Allows setting default types when chaining query.

Parameters

array $types

The array of types to set.

Returns

$this

getDefaultTypes()

getDefaultTypes() : array

Gets default types of current type map.

Returns

array

defaultTypes()

defaultTypes(array|null  $types = null) : $this|array

Allows setting default types when chaining query

Parameters

array|null $types

The array of types to set.

Returns

$this|array

__construct()

__construct(string|array|\Cake\Database\ExpressionInterface  $conditions = array(), array|\Cake\Database\TypeMap  $types = array(), string  $conjunction = 'AND') 

Constructor. A new expression object can be created without any params and be built dynamically. Otherwise it is possible to pass an array of conditions containing either a tree-like array structure to be parsed and/or other expression objects. Optionally, you can set the conjunction keyword to be used for joining each part of this level of the expression tree.

Parameters

string|array|\Cake\Database\ExpressionInterface $conditions

tree-like array structure containing all the conditions to be added or nested inside this expression object.

array|\Cake\Database\TypeMap $types

associative array of types to be associated with the values passed in $conditions.

string $conjunction

the glue that will join all the string conditions at this level of the expression tree. For example "AND", "OR", "XOR"...

setConjunction()

setConjunction(string  $conjunction) : $this

Changes the conjunction for the conditions at this level of the expression tree.

Parameters

string $conjunction

Value to be used for joining conditions

Returns

$this

getConjunction()

getConjunction() : string

Gets the currently configured conjunction for the conditions at this level of the expression tree.

Returns

string

tieWith()

tieWith(string|null  $conjunction = null) : string|$this

Changes the conjunction for the conditions at this level of the expression tree.

If called with no arguments it will return the currently configured value.

Parameters

string|null $conjunction

value to be used for joining conditions. If null it will not set any value, but return the currently stored one

Returns

string|$this

type()

type(string|null  $conjunction = null) : string|$this

Backwards compatible wrapper for tieWith()

Parameters

string|null $conjunction

value to be used for joining conditions. If null it will not set any value, but return the currently stored one

Returns

string|$this

add()

add(string|array|\Cake\Database\ExpressionInterface  $conditions, array  $types = array()) : $this

Adds one or more conditions to this expression object. Conditions can be expressed in a one dimensional array, that will cause all conditions to be added directly at this level of the tree or they can be nested arbitrarily making it create more expression objects that will be nested inside and configured to use the specified conjunction.

If the type passed for any of the fields is expressed "type[]" (note braces) then it will cause the placeholder to be re-written dynamically so if the value is an array, it will create as many placeholders as values are in it.

Parameters

string|array|\Cake\Database\ExpressionInterface $conditions

single or multiple conditions to be added. When using an array and the key is 'OR' or 'AND' a new expression object will be created with that conjunction and internal array value passed as conditions.

array $types

associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.

Returns

$this

eq()

eq(string|\Cake\Database\ExpressionInterface  $field, mixed  $value, string|null  $type = null) : $this

Adds a new condition to the expression object in the form "field = value".

Parameters

string|\Cake\Database\ExpressionInterface $field

Database field to be compared against value

mixed $value

The value to be bound to $field for comparison

string|null $type

the type name for $value as configured using the Type map. If it is suffixed with "[]" and the value is an array then multiple placeholders will be created, one per each value in the array.

Returns

$this

notEq()

notEq(string|\Cake\Database\ExpressionInterface  $field, mixed  $value, string|null  $type = null) : $this

Adds a new condition to the expression object in the form "field != value".

Parameters

string|\Cake\Database\ExpressionInterface $field

Database field to be compared against value

mixed $value

The value to be bound to $field for comparison

string|null $type

the type name for $value as configured using the Type map. If it is suffixed with "[]" and the value is an array then multiple placeholders will be created, one per each value in the array.

Returns

$this

gt()

gt(string|\Cake\Database\ExpressionInterface  $field, mixed  $value, string|null  $type = null) : $this

Adds a new condition to the expression object in the form "field > value".

Parameters

string|\Cake\Database\ExpressionInterface $field

Database field to be compared against value

mixed $value

The value to be bound to $field for comparison

string|null $type

the type name for $value as configured using the Type map.

Returns

$this

lt()

lt(string|\Cake\Database\ExpressionInterface  $field, mixed  $value, string|null  $type = null) : $this

Adds a new condition to the expression object in the form "field < value".

Parameters

string|\Cake\Database\ExpressionInterface $field

Database field to be compared against value

mixed $value

The value to be bound to $field for comparison

string|null $type

the type name for $value as configured using the Type map.

Returns

$this

gte()

gte(string|\Cake\Database\ExpressionInterface  $field, mixed  $value, string|null  $type = null) : $this

Adds a new condition to the expression object in the form "field >= value".

Parameters

string|\Cake\Database\ExpressionInterface $field

Database field to be compared against value

mixed $value

The value to be bound to $field for comparison

string|null $type

the type name for $value as configured using the Type map.

Returns

$this

lte()

lte(string|\Cake\Database\ExpressionInterface  $field, mixed  $value, string|null  $type = null) : $this

Adds a new condition to the expression object in the form "field <= value".

Parameters

string|\Cake\Database\ExpressionInterface $field

Database field to be compared against value

mixed $value

The value to be bound to $field for comparison

string|null $type

the type name for $value as configured using the Type map.

Returns

$this

isNull()

isNull(string|\Cake\Database\ExpressionInterface  $field) : $this

Adds a new condition to the expression object in the form "field IS NULL".

Parameters

string|\Cake\Database\ExpressionInterface $field

database field to be tested for null

Returns

$this

isNotNull()

isNotNull(string|\Cake\Database\ExpressionInterface  $field) : $this

Adds a new condition to the expression object in the form "field IS NOT NULL".

Parameters

string|\Cake\Database\ExpressionInterface $field

database field to be tested for not null

Returns

$this

like()

like(string|\Cake\Database\ExpressionInterface  $field, mixed  $value, string|null  $type = null) : $this

Adds a new condition to the expression object in the form "field LIKE value".

Parameters

string|\Cake\Database\ExpressionInterface $field

Database field to be compared against value

mixed $value

The value to be bound to $field for comparison

string|null $type

the type name for $value as configured using the Type map.

Returns

$this

notLike()

notLike(string|\Cake\Database\ExpressionInterface  $field, mixed  $value, string|null  $type = null) : $this

Adds a new condition to the expression object in the form "field NOT LIKE value".

Parameters

string|\Cake\Database\ExpressionInterface $field

Database field to be compared against value

mixed $value

The value to be bound to $field for comparison

string|null $type

the type name for $value as configured using the Type map.

Returns

$this

in()

in(string|\Cake\Database\ExpressionInterface  $field, string|array  $values, string|null  $type = null) : $this

Adds a new condition to the expression object in the form "field IN (value1, value2)".

Parameters

string|\Cake\Database\ExpressionInterface $field

Database field to be compared against value

string|array $values

the value to be bound to $field for comparison

string|null $type

the type name for $value as configured using the Type map.

Returns

$this

addCase()

addCase(array|\Cake\Database\ExpressionInterface  $conditions, array|\Cake\Database\ExpressionInterface  $values = array(), array  $types = array()) : $this

Adds a new case expression to the expression object

Parameters

array|\Cake\Database\ExpressionInterface $conditions

The conditions to test. Must be a ExpressionInterface instance, or an array of ExpressionInterface instances.

array|\Cake\Database\ExpressionInterface $values

associative array of values to be associated with the conditions passed in $conditions. If there are more $values than $conditions, the last $value is used as the ELSE value

array $types

associative array of types to be associated with the values passed in $values

Returns

$this

notIn()

notIn(string|\Cake\Database\ExpressionInterface  $field, array  $values, string|null  $type = null) : $this

Adds a new condition to the expression object in the form "field NOT IN (value1, value2)".

Parameters

string|\Cake\Database\ExpressionInterface $field

Database field to be compared against value

array $values

the value to be bound to $field for comparison

string|null $type

the type name for $value as configured using the Type map.

Returns

$this

exists()

exists(\Cake\Database\ExpressionInterface  $query) : $this

Adds a new condition to the expression object in the form "EXISTS (.

..)".

Parameters

\Cake\Database\ExpressionInterface $query

the inner query

Returns

$this

notExists()

notExists(\Cake\Database\ExpressionInterface  $query) : $this

Adds a new condition to the expression object in the form "NOT EXISTS (.

..)".

Parameters

\Cake\Database\ExpressionInterface $query

the inner query

Returns

$this

between()

between(string|\Cake\Database\ExpressionInterface  $field, mixed  $from, mixed  $to, string|null  $type = null) : $this

Adds a new condition to the expression object in the form "field BETWEEN from AND to".

Parameters

string|\Cake\Database\ExpressionInterface $field

The field name to compare for values in between the range.

mixed $from

The initial value of the range.

mixed $to

The ending value in the comparison range.

string|null $type

the type name for $value as configured using the Type map.

Returns

$this

and_()

and_(string|array|\Cake\Database\ExpressionInterface  $conditions, array  $types = array()) : \Cake\Database\Expression\QueryExpression

Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "AND"

Parameters

string|array|\Cake\Database\ExpressionInterface $conditions

to be joined with AND

array $types

associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.

Returns

\Cake\Database\Expression\QueryExpression

or_()

or_(string|array|\Cake\Database\ExpressionInterface  $conditions, array  $types = array()) : \Cake\Database\Expression\QueryExpression

Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "OR"

Parameters

string|array|\Cake\Database\ExpressionInterface $conditions

to be joined with OR

array $types

associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.

Returns

\Cake\Database\Expression\QueryExpression

not()

not(string|array|\Cake\Database\ExpressionInterface  $conditions, array  $types = array()) : $this

Adds a new set of conditions to this level of the tree and negates the final result by prepending a NOT, it will look like "NOT ( (condition1) AND (conditions2) )" conjunction depends on the one currently configured for this object.

Parameters

string|array|\Cake\Database\ExpressionInterface $conditions

to be added and negated

array $types

associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.

Returns

$this

count()

count() : integer

Returns the number of internal conditions that are stored in this expression.

Useful to determine if this expression object is void or it will generate a non-empty string when compiled

Returns

integer

equalFields()

equalFields(string  $left, string  $right) : $this

Builds equal condition or assignment with identifier wrapping.

Parameters

string $left

Left join condition field name.

string $right

Right join condition field name.

Returns

$this

sql()

sql(\Cake\Database\ValueBinder  $generator) : string

Returns the string representation of this object so that it can be used in a SQL query. Note that values condition values are not included in the string, in their place placeholders are put and can be replaced by the quoted values accordingly.

Parameters

\Cake\Database\ValueBinder $generator

Placeholder generator object

Returns

string

traverse()

traverse(callable  $callable) : void

Traverses the tree structure of this query expression by executing a callback function for each of the conditions that are included in this object.

Useful for compiling the final expression, or doing introspection in the structure.

Callback function receives as only argument an instance of ExpressionInterface

Parameters

callable $callable

The callable to apply to all sub-expressions.

iterateParts()

iterateParts(callable  $callable) : $this

Executes a callable function for each of the parts that form this expression.

The callable function is required to return a value with which the currently visited part will be replaced. If the callable function returns null then the part will be discarded completely from this expression.

The callback function will receive each of the conditions as first param and the key as second param. It is possible to declare the second parameter as passed by reference, this will enable you to change the key under which the modified part is stored.

Parameters

callable $callable

The callable to apply to each part.

Returns

$this

__call()

__call(string  $method, array  $args) : \Cake\Database\Expression\QueryExpression

Helps calling the `and()` and `or()` methods transparently.

Parameters

string $method

The method name.

array $args

The arguments to pass to the method.

Throws

\BadMethodCallException

Returns

\Cake\Database\Expression\QueryExpression

isCallable()

isCallable(callable  $c) : boolean

Check whether or not a callable is acceptable.

We don't accept ['class', 'method'] style callbacks, as they often contain user input and arrays of strings are easy to sneak in.

Parameters

callable $c

The callable to check.

Returns

boolean —

Valid callable.

hasNestedExpression()

hasNestedExpression() : boolean

Returns true if this expression contains any other nested ExpressionInterface objects

Returns

boolean

__clone()

__clone() : void

Clone this object and its subtree of expressions.

_addConditions()

_addConditions(array  $conditions, array  $types) : void

Auxiliary function used for decomposing a nested array of conditions and build a tree structure inside this object to represent the full SQL expression.

String conditions are stored directly in the conditions, while any other representation is wrapped around an adequate instance or of this class.

Parameters

array $conditions

list of conditions to be stored in this object

array $types

list of types associated on fields referenced in $conditions

_parseCondition()

_parseCondition(string  $field, mixed  $value) : string|\Cake\Database\ExpressionInterface

Parses a string conditions by trying to extract the operator inside it if any and finally returning either an adequate QueryExpression object or a plain string representation of the condition. This function is responsible for generating the placeholders and replacing the values by them, while storing the value elsewhere for future binding.

Parameters

string $field

The value from with the actual field and operator will be extracted.

mixed $value

The value to be bound to a placeholder for the field

Returns

string|\Cake\Database\ExpressionInterface

_calculateType()

_calculateType(string|\Cake\Database\Expression\IdentifierExpression  $field) : string|null

Returns the type name for the passed field if it was stored in the typeMap

Parameters

string|\Cake\Database\Expression\IdentifierExpression $field

The field name to get a type for.

Returns

string|null —

The computed type or null, if the type is unknown.