Class yii\db\JsonExpression
| 继承 | yii\db\JsonExpression |
|---|---|
| 实现 | JsonSerializable, yii\db\ExpressionInterface |
| 可用版本自 | 2.0.14 |
| 源码 | https://github.com/yiichina/yii2/blob/api/framework/db/JsonExpression.php |
JsonExpression 类表示应编码为 JSON 的数据。
例如:
new JsonExpression(['a' => 1, 'b' => 2]); // will be encoded to '{"a": 1, "b": 2}'
受保护的属性
| 属性 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| $type | string|null | 表达式应该被转为的 JSON 的类型。默认为 null,
表示不会执行显式转换。
只有支持不同类型 JSON 的 DBMS 才会遇到此属性。
例如,PostgreSQL 有 json 和 jsonb 类型。 |
yii\db\JsonExpression |
| $value | mixed | 要编码为 JSON 的值。 该值必须与 [\yii\helpers\Json::encode()|Json::encode()]] 输入要求兼容。 | yii\db\JsonExpression |
公共方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| __construct() | JsonExpression 构造函数。 | yii\db\JsonExpression |
| getType() | yii\db\JsonExpression | |
| getValue() | yii\db\JsonExpression | |
| jsonSerialize() | 指定应序列化为 JSON 的数据 | yii\db\JsonExpression |
常量
| 常量 | 值 | 描述 | 被定义在 |
|---|---|---|---|
| TYPE_JSON | 'json' | yii\db\JsonExpression | |
| TYPE_JSONB | 'jsonb' | yii\db\JsonExpression |
属性详情
表达式应该被转为的 JSON 的类型。默认为 null,
表示不会执行显式转换。
只有支持不同类型 JSON 的 DBMS 才会遇到此属性。
例如,PostgreSQL 有 json 和 jsonb 类型。
要编码为 JSON 的值。 该值必须与 [\yii\helpers\Json::encode()|Json::encode()]] 输入要求兼容。
方法详情
JsonExpression 构造函数。
参见 $type.
| public void __construct($value, $type = null) | ||
| $value | mixed | 要编码为 JSON 的值。 该值必须与 [\yii\helpers\Json::encode()|Json::encode()]] 要求兼容。 |
| $type | string|null | JSON 的类型。请参阅 yii\db\JsonExpression::$type |
参见 $type.
| public null|string getType() | ||
| return | null|string | JSON 的类型 |
|---|---|---|
参见 $value.
| public mixed getValue() |
指定应序列化为 JSON 的数据
| public mixed jsonSerialize() | ||
| return | mixed | 可以通过 json_encode 序列化的数据, 它是除资源之外的任何类型的值。 |
|---|---|---|
| throws | yii\base\InvalidConfigException | 当 JsonExpression 包含 QueryInterface 对象时抛出的异常 |