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 dateExpression 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.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$expression | string | The DB expression Default: null. | Expression |
$params | array | List of parameters that should be bound for this expression. Default: []. | Expression |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | Expression |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__toString() | String magic method. | Expression |
__unset() | Sets an object property to null. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
Property details
Section titled “Property details”$params
Section titled “$params ”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.
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(string $expression, array $params = [], array $config = [])Constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$expression | string | The DB expression |
$params | array | Parameters |
$config | array | Name-value pairs that will be used to initialize the object properties |
__toString()
Section titled “__toString() ”public function __toString(): stringString magic method.
Returns
string: The DB expression.