類別 yii\db\JsonExpression
繼承 | yii\db\JsonExpression |
---|---|
實作 | JsonSerializable, yii\db\ExpressionInterface |
自版本起可用 | 2.0.14 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/db/JsonExpression.php |
JsonExpression 類別代表應編碼為 JSON 的資料。
例如
new JsonExpression(['a' => 1, 'b' => 2]); // will be encoded to '{"a": 1, "b": 2}'
公開方法
方法 | 描述 | 定義於 |
---|---|---|
__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 |
public function __construct($value, $type = null)
{
if ($value instanceof self) {
$value = $value->getValue();
}
$this->value = $value;
$this->type = $type;
}
另請參閱 $type。
public string|null getType ( ) | ||
return | string|null |
JSON 的類型 |
---|
public function getType()
{
return $this->type;
}
指定應序列化為 JSON 的資料
public mixed jsonSerialize ( ) | ||
return | mixed |
可被 json_encode 序列化的資料,其為任何非資源型別的值。 |
---|---|---|
throws | yii\base\InvalidConfigException |
當 JsonExpression 包含 QueryInterface 物件時 |
#[\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;
}
註冊 或 登入 以進行評論。