0 follower

Class yii\db\JsonExpression

Inheritanceyii\db\JsonExpression
ImplementsJsonSerializable, yii\db\ExpressionInterface
Available since version2.0.14
Source Code https://github.com/yiisoft/yii2/blob/master/framework/db/JsonExpression.php

Class JsonExpression represents data that should be encoded to JSON.

For example:

new JsonExpression(['a' => 1, 'b' => 2]); // will be encoded to '{"a": 1, "b": 2}'

Protected Properties

Hide inherited properties

Property Type Description Defined By

Public Methods

Hide inherited methods

Method Description Defined By
__construct() JsonExpression constructor. yii\db\JsonExpression
getType() yii\db\JsonExpression
getValue() yii\db\JsonExpression
jsonSerialize() Specify data which should be serialized to JSON yii\db\JsonExpression

Constants

Hide inherited constants

Constant Value Description Defined By
TYPE_JSON 'json' yii\db\JsonExpression
TYPE_JSONB 'jsonb' yii\db\JsonExpression

Property Details

Hide inherited properties

$type protected property

Type of JSON, expression should be casted to. Defaults to null, meaning no explicit casting will be performed. This property will be encountered only for DBMSs that support different types of JSON. For example, PostgreSQL has json and jsonb types.

protected string|null $type null
$value protected property

The value to be encoded to JSON. The value must be compatible with [\yii\helpers\Json::encode()|Json::encode()]] input requirements.

protected mixed $value null

Method Details

Hide inherited methods

__construct() public method

JsonExpression constructor.

See also $type.

public void __construct ( $value, $type null )
$value mixed

The value to be encoded to JSON. The value must be compatible with [\yii\helpers\Json::encode()|Json::encode()]] requirements.

$type string|null

The type of the JSON. See yii\db\JsonExpression::$type

                public function __construct($value, $type = null)
{
    if ($value instanceof self) {
        $value = $value->getValue();
    }
    $this->value = $value;
    $this->type = $type;
}

            
getType() public method

See also $type.

public string|null getType ( )
return string|null

The type of JSON

                public function getType()
{
    return $this->type;
}

            
getValue() public method

See also $value.

public mixed getValue ( )

                public function getValue()
{
    return $this->value;
}

            
jsonSerialize() public method (available since version 2.0.14.2)

Specify data which should be serialized to JSON

public mixed jsonSerialize ( )
return mixed

Data which can be serialized by json_encode, which is a value of any type other than a resource.

throws yii\base\InvalidConfigException

when JsonExpression contains QueryInterface object

                #[\ReturnTypeWillChange]
public function jsonSerialize()
{
    $value = $this->getValue();
    if ($value instanceof QueryInterface) {
        throw new InvalidConfigException('The JsonExpression class can not be serialized to JSON when the value is a QueryInterface object');
    }
    return $value;
}