0 追蹤者

類別 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

屬性詳細資訊

隱藏繼承的屬性

$type 受保護的屬性

JSON 的類型,表示式應轉換為此類型。預設為 null,表示不會執行明確的轉換。此屬性僅在支援不同 JSON 類型的 DBMS 中才會遇到。例如,PostgreSQL 具有 jsonjsonb 類型。

protected string|null $type null
$value protected property

要編碼為 JSON 的值。此值必須與 [\yii\helpers\Json::encode()|Json::encode()]] 輸入需求相容。

protected mixed $value null

方法詳情

隱藏繼承的方法

__construct() public method

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;
}

            
getType() public method

另請參閱 $type

public string|null getType ( )
return string|null

JSON 的類型

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

            
getValue() public method

另請參閱 $value

public mixed getValue ( )

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

            
jsonSerialize() public method (available since version 2.0.14.2)

指定應序列化為 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;
}