Skip to content
PHP Ecosystem
Explore

JsonExpression

API reference › yii › db › JsonExpression

classyii\db\JsonExpression
ImplementsExpressionInterface, JsonSerializable
Since2.0.14
Sourceframework/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}'
PropertyTypeDescriptionDefined by
$type protectedstring|nullType of JSON, expression should be casted to. Default: null.JsonExpression
$value protectedmixedThe value to be encoded to JSON. Default: null.JsonExpression
MethodDescriptionDefined by
__construct()JsonExpression constructor.JsonExpression
getType()JsonExpression
getValue()JsonExpression
jsonSerialize()Specify data which should be serialized to JSONJsonExpression
ConstantValueDescriptionDefined by
TYPE_JSON'json'JsonExpression
TYPE_JSONB'jsonb'JsonExpression
protected string|null $type = null

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 mixed $value = null

The value to be encoded to JSON.

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

public function __construct(mixed $value, string|null $type = null)

JsonExpression constructor.

Parameters

ParameterTypeDescription
$valuemixedThe value to be encoded to JSON. The value must be compatible with [\yii\helpers\Json::encode()|Json::encode()]] requirements.
$typestring|nullThe type of the JSON. See JsonExpression::$type

See also

public function getType(): string|null

Returns

string|null: The type of JSON

See also

public function getValue(): mixed

Returns

mixed

See also

public function jsonSerialize(): mixed

Specify data which should be serialized to JSON

Returns

mixed: Data which can be serialized by <b>json_encode</b>, which is a value of any type other than a resource.

Throws

Since

2.0.14.2