Class FunctionsBuilder

Contains methods related to generating FunctionExpression objects with most commonly used SQL functions.

This acts as a factory for FunctionExpression objects.

Namespace: Cake\Database

Method Summary

  • __call() public

    Magic method dispatcher to create custom SQL function calls

  • aggregate() public

    Helper method to create arbitrary SQL aggregate function calls.

  • avg() public

    Returns a AggregateExpression representing a call to SQL AVG function.

  • cast() public

    Returns a FunctionExpression representing a SQL CAST.

  • coalesce() public

    Returns a FunctionExpression representing a call to SQL COALESCE function.

  • concat() public

    Returns a FunctionExpression representing a string concatenation

  • count() public

    Returns a AggregateExpression representing a call to SQL COUNT function.

  • dateAdd() public

    Add the time unit to the date expression

  • dateDiff() public

    Returns a FunctionExpression representing the difference in days between two dates.

  • datePart() public

    Returns the specified date part from the SQL expression.

  • dayOfWeek() public

    Returns a FunctionExpression representing a call to SQL WEEKDAY function.

  • extract() public

    Returns the specified date part from the SQL expression.

  • lag() public

    Returns an AggregateExpression representing call to SQL LAG().

  • lead() public

    Returns an AggregateExpression representing call to SQL LEAD().

  • max() public

    Returns a AggregateExpression representing a call to SQL MAX function.

  • min() public

    Returns a AggregateExpression representing a call to SQL MIN function.

  • now() public

    Returns a FunctionExpression representing a call that will return the current date and time. By default it returns both date and time, but you can also make it generate only the date or only the time.

  • rand() public

    Returns a FunctionExpression representing a call to SQL RAND function.

  • rowNumber() public

    Returns an AggregateExpression representing call to SQL ROW_NUMBER().

  • sum() public

    Returns a AggregateExpression representing a call to SQL SUM function.

  • toLiteralParam() protected

    Creates function parameter array from expression or string literal.

  • weekday() public

    Returns a FunctionExpression representing a call to SQL WEEKDAY function.

Method Detail

__call() public

__call(string $name, array $args)

Magic method dispatcher to create custom SQL function calls

Parameters

string $name

the SQL function name to construct

array $args

list with up to 3 arguments, first one being an array with parameters for the SQL function, the second one a list of types to bind to those params, and the third one the return type of the function

Returns

\Cake\Database\Expression\FunctionExpression

aggregate() public

aggregate(string $name, array $params, array $types, string $return)

Helper method to create arbitrary SQL aggregate function calls.

Parameters

string $name

The SQL aggregate function name

array $params optional

Array of arguments to be passed to the function. Can be an associative array with the literal value or identifier: ['value' => 'literal'] or `['value' => 'identifier']

array $types optional

Array of types that match the names used in $params: ['name' => 'type']

string $return optional

Return type of the entire expression. Defaults to float.

Returns

\Cake\Database\Expression\AggregateExpression

avg() public

avg(mixed $expression, mixed $types)

Returns a AggregateExpression representing a call to SQL AVG function.

Parameters

string|\Cake\Database\ExpressionInterface $expression

the function argument

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\AggregateExpression

cast() public

cast(mixed $field, string $type)

Returns a FunctionExpression representing a SQL CAST.

The $type parameter is a SQL type. The return type for the returned expression is the default type name. Use setReturnType() to update it.

Parameters

string|\Cake\Database\ExpressionInterface $field

Field or expression to cast.

string $type optional

The SQL data type

Returns

\Cake\Database\Expression\FunctionExpression

coalesce() public

coalesce(array $args, array $types)

Returns a FunctionExpression representing a call to SQL COALESCE function.

Parameters

array $args

List of expressions to evaluate as function parameters

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\FunctionExpression

concat() public

concat(array $args, array $types)

Returns a FunctionExpression representing a string concatenation

Parameters

array $args

List of strings or expressions to concatenate

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\FunctionExpression

count() public

count(mixed $expression, mixed $types)

Returns a AggregateExpression representing a call to SQL COUNT function.

Parameters

string|\Cake\Database\ExpressionInterface $expression

the function argument

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\AggregateExpression

dateAdd() public

dateAdd(mixed $expression, mixed $value, string $unit, array $types)

Add the time unit to the date expression

Parameters

string|\Cake\Database\ExpressionInterface $expression

Expression to obtain the date part from.

string|int $value

Value to be added. Use negative to subtract.

string $unit

Unit of the value e.g. hour or day.

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\FunctionExpression

dateDiff() public

dateDiff(array $args, array $types)

Returns a FunctionExpression representing the difference in days between two dates.

Parameters

array $args

List of expressions to obtain the difference in days.

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\FunctionExpression

datePart() public

datePart(string $part, mixed $expression, array $types)

Returns the specified date part from the SQL expression.

Parameters

string $part

Part of the date to return.

string|\Cake\Database\ExpressionInterface $expression

Expression to obtain the date part from.

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\FunctionExpression

dayOfWeek() public

dayOfWeek(mixed $expression, mixed $types)

Returns a FunctionExpression representing a call to SQL WEEKDAY function.

1 - Sunday, 2 - Monday, 3 - Tuesday...

Parameters

string|\Cake\Database\ExpressionInterface $expression

the function argument

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\FunctionExpression

extract() public

extract(string $part, mixed $expression, array $types)

Returns the specified date part from the SQL expression.

Parameters

string $part

Part of the date to return.

string|\Cake\Database\ExpressionInterface $expression

Expression to obtain the date part from.

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\FunctionExpression

lag() public

lag(mixed $expression, int $offset, mixed $default, mixed $type)

Returns an AggregateExpression representing call to SQL LAG().

Parameters

\Cake\Database\ExpressionInterface|string $expression

The value evaluated at offset

int $offset

The row offset

mixed $default optional

The default value if offset doesn't exist

string $type optional

The output type of the lag expression. Defaults to float.

Returns

\Cake\Database\Expression\AggregateExpression

lead() public

lead(mixed $expression, int $offset, mixed $default, mixed $type)

Returns an AggregateExpression representing call to SQL LEAD().

Parameters

\Cake\Database\ExpressionInterface|string $expression

The value evaluated at offset

int $offset

The row offset

mixed $default optional

The default value if offset doesn't exist

string $type optional

The output type of the lead expression. Defaults to float.

Returns

\Cake\Database\Expression\AggregateExpression

max() public

max(mixed $expression, mixed $types)

Returns a AggregateExpression representing a call to SQL MAX function.

Parameters

string|\Cake\Database\ExpressionInterface $expression

the function argument

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\AggregateExpression

min() public

min(mixed $expression, mixed $types)

Returns a AggregateExpression representing a call to SQL MIN function.

Parameters

string|\Cake\Database\ExpressionInterface $expression

the function argument

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\AggregateExpression

now() public

now(string $type)

Returns a FunctionExpression representing a call that will return the current date and time. By default it returns both date and time, but you can also make it generate only the date or only the time.

Parameters

string $type optional

(datetime|date|time)

Returns

\Cake\Database\Expression\FunctionExpression

rand() public

rand()

Returns a FunctionExpression representing a call to SQL RAND function.

Returns

\Cake\Database\Expression\FunctionExpression

rowNumber() public

rowNumber()

Returns an AggregateExpression representing call to SQL ROW_NUMBER().

Returns

\Cake\Database\Expression\AggregateExpression

sum() public

sum(mixed $expression, mixed $types)

Returns a AggregateExpression representing a call to SQL SUM function.

Parameters

string|\Cake\Database\ExpressionInterface $expression

the function argument

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\AggregateExpression

toLiteralParam() protected

toLiteralParam(mixed $expression)

Creates function parameter array from expression or string literal.

Parameters

\Cake\Database\ExpressionInterface|string $expression

function argument

Returns

(\Cake\Database\ExpressionInterface|string)[]

weekday() public

weekday(mixed $expression, mixed $types)

Returns a FunctionExpression representing a call to SQL WEEKDAY function.

1 - Sunday, 2 - Monday, 3 - Tuesday...

Parameters

string|\Cake\Database\ExpressionInterface $expression

the function argument

array $types optional

list of types to bind to the arguments

Returns

\Cake\Database\Expression\FunctionExpression

© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.1/class-Cake.Database.FunctionsBuilder.html