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 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 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
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • BetweenExpression
  • CaseExpression
  • Comparison
  • FunctionExpression
  • IdentifierExpression
  • OrderByExpression
  • OrderClauseExpression
  • QueryExpression
  • TupleComparison
  • UnaryExpression
  • ValuesExpression

Interfaces

  • FieldInterface

Traits

  • FieldTrait

Class FunctionExpression

This class represents a function call string in a SQL statement. Calls can be constructed by passing the name of the function and a list of params. For security reasons, all params passed are quoted by default unless explicitly told otherwise.

Cake\Database\Expression\QueryExpression implements Cake\Database\ExpressionInterface, Countable uses Cake\Database\TypeMapTrait
Extended by Cake\Database\Expression\FunctionExpression implements Cake\Database\TypedResultInterface uses Cake\Database\Type\ExpressionTypeCasterTrait , Cake\Database\TypedResultTrait
Namespace: Cake\Database\Expression
Location: Database/Expression/FunctionExpression.php

Properties summary

  • $_name protected
    string
    The name of the function to be constructed when generating the SQL string

Inherited Properties

  • _conditions, _conjunction _typeMap _returnType

Method Summary

  • __construct() public

    Constructor. Takes a name for the function to be invoked and a list of params to be passed into the function. Optionally you can pass a list of types to be used for each bound param.

  • add() public
    Adds one or more arguments for the function call.
  • count() public

    The name of the function is in itself an expression to generate, thus always adding 1 to the amount of expressions stored in this object.

  • getName() public
    Gets the name of the SQL function to be invoke in this expression.
  • name() public deprecated

    Sets the name of the SQL function to be invoke in this expression, if no value is passed it will return current name

  • setName() public
    Sets the name of the SQL function to be invoke in this expression.
  • sql() public

    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.

Method Detail

__construct() public ¶

__construct( string $name , array $params = [] , array $types = [] , string $returnType = 'string' )

Constructor. Takes a name for the function to be invoked and a list of params to be passed into the function. Optionally you can pass a list of types to be used for each bound param.

By default, all params that are passed will be quoted. If you wish to use literal arguments, you need to explicitly hint this function.

Examples:

$f = new FunctionExpression('CONCAT', ['CakePHP', ' rules']);

Previous line will generate CONCAT('CakePHP', ' rules')

$f = new FunctionExpression('CONCAT', ['name' => 'literal', ' rules']);

Will produce CONCAT(name, ' rules')

Parameters
string $name
the name of the function to be constructed
array $params optional []

list of arguments to be passed to the function If associative the key would be used as argument when value is 'literal'

array $types optional []

associative array of types to be associated with the passed arguments

string $returnType optional 'string'
The return type of this expression
Overrides
Cake\Database\Expression\QueryExpression::__construct()

add() public ¶

add( array $params , array $types = [] , boolean $prepend = false )

Adds one or more arguments for the function call.

Parameters
array $params

list of arguments to be passed to the function If associative the key would be used as argument when value is 'literal'

array $types optional []

associative array of types to be associated with the passed arguments

boolean $prepend optional false
Whether to prepend or append to the list of arguments
Returns

$this
See
\Cake\Database\Expression\FunctionExpression::__construct() for more details.
Overrides
Cake\Database\Expression\QueryExpression::add()

count() public ¶

count( )

The name of the function is in itself an expression to generate, thus always adding 1 to the amount of expressions stored in this object.

Returns
integer
Overrides
Cake\Database\Expression\QueryExpression::count()

getName() public ¶

getName( )

Gets the name of the SQL function to be invoke in this expression.

Returns
string

name() public deprecated ¶

name( string|null $name = null )

Sets the name of the SQL function to be invoke in this expression, if no value is passed it will return current name

Deprecated
3.4.0 Use setName()/getName() instead.
Parameters
string|null $name optional null
The name of the function
Returns
string|Cake\Database\Expression\FunctionExpression
$this

setName() public ¶

setName( string $name )

Sets the name of the SQL function to be invoke in this expression.

Parameters
string $name
The name of the function
Returns

$this

sql() public ¶

sql( Cake\Database\ValueBinder $generator )

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
Overrides
Cake\Database\Expression\QueryExpression::sql()

Methods inherited from Cake\Database\Expression\QueryExpression

__call() public ¶

__call( string $method , array $args )

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

Parameters
string $method
The method name.
array $args
The arguments to pass to the method.
Returns
Cake\Database\Expression\QueryExpression
Throws
BadMethodCallException

__clone() public ¶

__clone( )

Clone this object and its subtree of expressions.

_addConditions() protected ¶

_addConditions( array $conditions , array $types )

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

_calculateType() protected ¶

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

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.

_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

addCase() public ¶

addCase( array|Cake\Database\ExpressionInterface $conditions , array|Cake\Database\ExpressionInterface $values = [] , array $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_( callable|string|array|Cake\Database\ExpressionInterface $conditions , array $types = [] )

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

Parameters
callable|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( string|Cake\Database\ExpressionInterface $field , mixed $from , mixed $to , string|null $type = null )

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 optional null
the type name for $value as configured using the Type map.
Returns

$this

eq() public ¶

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

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 null

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

gt() public ¶

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

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 null
the type name for $value as configured using the Type map.
Returns

$this

gte() public ¶

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

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 null
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
boolean

in() public ¶

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

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 optional null
the type name for $value as configured using the Type map.
Returns

$this

isCallable() public ¶

isCallable( callable $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 $c
The callable to check.
Returns
boolean
Valid callable.

isNotNull() public ¶

isNotNull( string|Cake\Database\ExpressionInterface $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( string|Cake\Database\ExpressionInterface $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 $callable )

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

like() public ¶

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

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 null
the type name for $value as configured using the Type map.
Returns

$this

lt() public ¶

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

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 null
the type name for $value as configured using the Type map.
Returns

$this

lte() public ¶

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

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 null
the type name for $value as configured using the Type map.
Returns

$this

not() public ¶

not( string|array|Cake\Database\ExpressionInterface $conditions , array $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( string|Cake\Database\ExpressionInterface $field , mixed $value , string|null $type = null )

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 null

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( string|Cake\Database\ExpressionInterface $field , array $values , string|null $type = null )

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 optional null
the type name for $value as configured using the Type map.
Returns

$this

notLike() public ¶

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

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 null
the type name for $value as configured using the Type map.
Returns

$this

or_() public ¶

or_( callable|string|array|Cake\Database\ExpressionInterface $conditions , array $types = [] )

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

Parameters
callable|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

tieWith() public deprecated ¶

tieWith( string|null $conjunction = null )

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.

Deprecated
3.4.0 Use setConjunction()/getConjunction() instead.
Parameters
string|null $conjunction optional null

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

Returns
string|Cake\Database\Expression\QueryExpression
$this

traverse() public ¶

traverse( callable $callable )

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.
Implementation of
Cake\Database\ExpressionInterface::traverse()

type() public deprecated ¶

type( string|null $conjunction = null )

Backwards compatible wrapper for tieWith()

Deprecated
3.2.0 Use setConjunction()/getConjunction() instead
Parameters
string|null $conjunction optional null

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

Returns
string|Cake\Database\Expression\QueryExpression
$this

Methods used from Cake\Database\Type\ExpressionTypeCasterTrait

_castToExpression() protected ¶

_castToExpression( mixed $value , string $type )

Conditionally converts the passed value to an ExpressionInterface object if the type class implements the ExpressionTypeInterface. Otherwise, returns the value unmodified.

Parameters
mixed $value
The value to converto to ExpressionInterface
string $type
The type name
Returns
mixed

_requiresToExpressionCasting() protected ¶

_requiresToExpressionCasting( array $types )

Returns an array with the types that require values to be casted to expressions, out of the list of type names passed as parameter.

Parameters
array $types
List of type names
Returns
array

Methods used from Cake\Database\TypedResultTrait

getReturnType() public ¶

getReturnType( )

Gets the type of the value this object will generate.

Returns
string

returnType() public deprecated ¶

returnType( string|null $type = null )

Sets the type of the value this object will generate. If called without arguments, returns the current known type

Deprecated
3.5.0 Use getReturnType()/setReturnType() instead.
Parameters
string|null $type optional null
The name of the type that is to be returned
Returns
string|Cake\Database\TypedResultTrait
$this

setReturnType() public ¶

setReturnType( string $type )

Sets the type of the value this object will generate.

Parameters
string $type
The name of the type that is to be returned
Returns

$this

Methods used from Cake\Database\TypeMapTrait

defaultTypes() public deprecated ¶

defaultTypes( array $types = null )

Allows setting default types when chaining query

Deprecated
3.4.0 Use setDefaultTypes()/getDefaultTypes() instead.
Parameters
array $types optional null
The array of types to set.
Returns

$this|array

getDefaultTypes() public ¶

getDefaultTypes( )

Gets default types of current type map.

Returns
array

getTypeMap() public ¶

getTypeMap( )

Returns the existing type map.

Returns
Cake\Database\TypeMap

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
\Cake\Database\TypeMap::setDefaults()

setTypeMap() public ¶

setTypeMap( array|Cake\Database\TypeMap $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

typeMap() public deprecated ¶

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

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

Deprecated
3.4.0 Use setTypeMap()/getTypeMap() instead.
Parameters
array|Cake\Database\TypeMap|null $typeMap optional null
Creates a TypeMap if array, otherwise sets the given TypeMap
Returns

$this|\Cake\Database\TypeMap

Magic methods inherited from Cake\Database\TypedResultInterface

getReturnType()

Properties detail

$_name ¶

protected string

The name of the function to be constructed when generating the SQL string

Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • 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

Generated using CakePHP API Docs