CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 4.0 Strawberry API

  • Overview
  • Version:
    • 4.0
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
      • Dialect
      • Driver
      • Exception
      • Expression
      • Log
      • Retry
      • Schema
      • Statement
      • Type
    • Datasource
    • Error
    • Event
    • Filesystem
    • Form
    • Http
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • Shell
    • TestSuite
    • Utility
    • Validation
    • View

Class OrderByExpression

An expression object for ORDER BY clauses

Namespace: Cake\Database\Expression

Properties summary

  • $_conditions protected
    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"

  • $_conjunction protected
    string

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

  • $_typeMap protected
    \Cake\Database\TypeMap

Method Summary

  • __clone() public

    Clone this object and its subtree of expressions.

  • __construct() public

    Constructor

  • _addConditions() protected

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

  • _calculateType() protected

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

  • _parseCondition() protected

    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.

  • add() public

    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.

  • addCase() public

    Adds a new case expression to the expression object

  • and() public

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

  • and_() public

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

  • between() public

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

  • count() public

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

  • eq() public

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

  • equalFields() public

    Builds equal condition or assignment with identifier wrapping.

  • exists() public

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

  • getConjunction() public

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

  • getDefaultTypes() public

    Gets default types of current type map.

  • getTypeMap() public

    Returns the existing type map.

  • gt() public

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

  • gte() public

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

  • hasNestedExpression() public

    Returns true if this expression contains any other nested ExpressionInterface objects

  • in() public

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

  • isCallable() public

    Check whether or not a callable is acceptable.

  • isNotNull() public

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

  • isNull() public

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

  • iterateParts() public

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

  • like() public

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

  • lt() public

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

  • lte() public

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

  • not() public

    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.

  • notEq() public

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

  • notExists() public

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

  • notIn() public

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

  • notLike() public

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

  • or() public

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

  • or_() public

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

  • setConjunction() public

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

  • setDefaultTypes() public

    Overwrite the default type mappings for fields in the implementing object.

  • setTypeMap() public

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

  • sql() public

    Convert the expression into a SQL fragment.

  • traverse() public

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

Method Detail

__clone() public

¶

__clone()

Clone this object and its subtree of expressions.

__construct() public

¶

__construct(mixed $conditions, mixed $types, mixed $conjunction)

Constructor

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

The sort columns

array|\Cake\Database\TypeMap $types optional

The types for each column.

string $conjunction optional

The glue used to join conditions together.

_addConditions() protected

¶

_addConditions(array $orders, array $types)

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

New order by expressions are merged to existing ones

Parameters
array $orders

list of order by expressions

array $types

list of types associated on fields referenced in $conditions

_calculateType() protected

¶

_calculateType(mixed $field)

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

Parameters
string|\Cake\Database\ExpressionInterface $field

The field name to get a type for.

Returns
string|null

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

_parseCondition() protected

¶

_parseCondition(string $field, mixed $value)

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
Throws
InvalidArgumentException
If operator is invalid or missing on NULL usage.

add() public

¶

add(mixed $conditions, array $types)

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 optional

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

Returns
$this
See Also
\Cake\Database\Query::where()
for examples on conditions

addCase() public

¶

addCase(mixed $conditions, mixed $values, mixed $types)

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 optional

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 optional

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

Returns
$this

and() public

¶

and(mixed $conditions, mixed $types)

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

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

to be joined with AND

array $types optional

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

and_() public

¶

and_(mixed $conditions, mixed $types)

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

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

to be joined with AND

array $types optional

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

between() public

¶

between(mixed $field, mixed $from, mixed $to, mixed $type)

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 inbetween the range.

mixed $from

The initial value of the range.

mixed $to

The ending value in the comparison range.

string|null $type optional

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

Returns
$this

count() public

¶

count()

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
int

eq() public

¶

eq(mixed $field, mixed $value, ?string $type)

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 optional

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

equalFields() public

¶

equalFields(string $left, string $right)

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

exists() public

¶

exists(\Cake\Database\ExpressionInterface $query)

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

Parameters
\Cake\Database\ExpressionInterface $query

the inner query

Returns
$this

getConjunction() public

¶

getConjunction()

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

Returns
string

getDefaultTypes() public

¶

getDefaultTypes()

Gets default types of current type map.

Returns
array

getTypeMap() public

¶

getTypeMap()

Returns the existing type map.

Returns
\Cake\Database\TypeMap

gt() public

¶

gt(mixed $field, mixed $value, mixed $type)

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 optional

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

Returns
$this

gte() public

¶

gte(mixed $field, mixed $value, mixed $type)

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 optional

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

Returns
$this

hasNestedExpression() public

¶

hasNestedExpression()

Returns true if this expression contains any other nested ExpressionInterface objects

Returns
bool

in() public

¶

in(mixed $field, mixed $values, mixed $type)

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|\Cake\Database\ExpressionInterface $values

the value to be bound to $field for comparison

string|null $type optional

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

Returns
$this

isCallable() public

¶

isCallable(mixed $c)

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|string|array|\Cake\Database\ExpressionInterface $c

The callable to check.

Returns
bool

Valid callable.

isNotNull() public

¶

isNotNull(mixed $field)

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

isNull() public

¶

isNull(mixed $field)

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

iterateParts() public

¶

iterateParts(callable $visitor)

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 $visitor

The callable to apply to each part.

Returns
$this

like() public

¶

like(mixed $field, mixed $value, mixed $type)

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 optional

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

Returns
$this

lt() public

¶

lt(mixed $field, mixed $value, mixed $type)

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 optional

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

Returns
$this

lte() public

¶

lte(mixed $field, mixed $value, mixed $type)

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 optional

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

Returns
$this

not() public

¶

not(mixed $conditions, mixed $types)

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 optional

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

Returns
$this

notEq() public

¶

notEq(mixed $field, mixed $value, mixed $type)

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 optional

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

notExists() public

¶

notExists(\Cake\Database\ExpressionInterface $query)

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

Parameters
\Cake\Database\ExpressionInterface $query

the inner query

Returns
$this

notIn() public

¶

notIn(mixed $field, mixed $values, mixed $type)

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|\Cake\Database\ExpressionInterface $values

the value to be bound to $field for comparison

string|null $type optional

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

Returns
$this

notLike() public

¶

notLike(mixed $field, mixed $value, mixed $type)

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 optional

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

Returns
$this

or() public

¶

or(mixed $conditions, mixed $types)

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

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

to be joined with OR

array $types optional

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_() public

¶

or_(mixed $conditions, mixed $types)

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

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

to be joined with OR

array $types optional

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

setConjunction() public

¶

setConjunction(string $conjunction)

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

setDefaultTypes() public

¶

setDefaultTypes(array $types)

Overwrite the default type mappings for fields in the implementing object.

This method is useful if you need to set type mappings that are shared across multiple functions/expressions in a query.

To add a default without overwriting existing ones use getTypeMap()->addDefaults()

Parameters
array $types

The array of types to set.

Returns
$this
See Also
\Cake\Database\TypeMap::setDefaults()

setTypeMap() public

¶

setTypeMap(mixed $typeMap)

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

sql() public

¶

sql(\Cake\Database\ValueBinder $generator)

Convert the expression into a SQL fragment.

Parameters
\Cake\Database\ValueBinder $generator

Placeholder generator object

Returns
string

traverse() public

¶

traverse(\Closure $visitor)

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
\Closure $visitor

The callable to apply to all sub-expressions.

Returns
$this

Property Detail

$_conditions ¶ protected

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

$_conjunction ¶ protected

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

Type
string

$_typeMap ¶ protected

Type
\Cake\Database\TypeMap