Skip to content
PHP Ecosystem
Explore

Expression

API reference › yii › db › Expression

Expression represents a DB expression that does not need escaping or quoting.

When an Expression object is embedded within a SQL statement or fragment, it will be replaced with the $expression property value without any DB escaping or quoting. For example,

$expression = new Expression('NOW()');
$now = (new \yii\db\Query)->select($expression)->scalar(); // SELECT NOW();
echo $now; // prints the current date

Expression objects are mainly created for passing raw SQL expressions to methods of Query, ActiveQuery, and related classes.

An expression can also be bound with parameters specified via $params.

PropertyTypeDescriptionDefined by
$expressionstringThe DB expression Default: null.Expression
$paramsarrayList of parameters that should be bound for this expression. Default: [].Expression
MethodDescriptionDefined by
__construct()Constructor.Expression
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__toString()String magic method.Expression
__unset()Sets an object property to null.yii\base\BaseObject
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
public array $params = []

List of parameters that should be bound for this expression.

The keys are placeholders appearing in $expression and the values are the corresponding parameter values.

public function __construct(string $expression, array $params = [], array $config = [])

Constructor.

Parameters

ParameterTypeDescription
$expressionstringThe DB expression
$paramsarrayParameters
$configarrayName-value pairs that will be used to initialize the object properties
public function __toString(): string

String magic method.

Returns

string: The DB expression.