0 關注者

類別 yii\db\Expression

繼承yii\db\Expression » yii\base\BaseObject
實作yii\base\Configurable, yii\db\ExpressionInterface
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/db/Expression.php

Expression 代表一個資料庫表達式,不需要轉義或引用。

當 Expression 物件嵌入在 SQL 語句或片段中時,它將被替換為 $expression 屬性值,而不會進行任何資料庫轉義或引用。例如:

$expression = new Expression('NOW()');
$now = (new \yii\db\Query)->select($expression)->scalar();  // SELECT NOW();
echo $now; // prints the current date

Expression 物件主要用於將原始 SQL 表達式傳遞給 yii\db\Queryyii\db\ActiveQuery 和相關類別的方法。

表達式也可以透過 $params 指定的參數進行綁定。

公共屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$expression 字串 資料庫表達式 yii\db\Expression
$params 陣列 應為此表達式綁定的參數列表。 yii\db\Expression

公共方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫未作為類別方法的具名方法。 yii\base\BaseObject
__construct() 建構子。 yii\db\Expression
__get() 回傳物件屬性的值。 yii\base\BaseObject
__isset() 檢查屬性是否已設定,即已定義且非 null。 yii\base\BaseObject
__set() 設定物件屬性的值。 yii\base\BaseObject
__toString() 字串魔術方法。 yii\db\Expression
__unset() 將物件屬性設定為 null。 yii\base\BaseObject
canGetProperty() 回傳一個值,指示是否可以讀取屬性。 yii\base\BaseObject
canSetProperty() 回傳一個值,指示是否可以設定屬性。 yii\base\BaseObject
className() 回傳此類別的完整限定名稱。 yii\base\BaseObject
hasMethod() 回傳一個值,指示是否已定義方法。 yii\base\BaseObject
hasProperty() 傳回值,指出是否已定義屬性。 yii\base\BaseObject
init() 初始化物件。 yii\base\BaseObject

屬性詳情

隱藏繼承的屬性

$expression public property

資料庫表達式

public string $expression null
$params public property

應为此表達式綁定的參數列表。鍵是在 $expression 中顯示的佔位符,值是相應的參數值。

public array $params = []

方法詳情

隱藏繼承的方法

__call() public method

定義於: yii\base\BaseObject::__call()

呼叫未作為類別方法的具名方法。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當調用未知方法時,將會隱式呼叫此方法。

public mixed __call ( $name, $params )
$name 字串

方法名稱

$params 陣列

$params

return mixed

方法傳回值

throws yii\base\UnknownMethodException

當呼叫未知方法時

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

建構子。

public void __construct ( $expression, $params = [], $config = [] )
$expression 字串

資料庫表達式

$params 陣列

參數

$config 陣列

將用於初始化物件屬性的名稱-值對

                public function __construct($expression, $params = [], $config = [])
{
    $this->expression = $expression;
    $this->params = $params;
    parent::__construct($config);
}

            
__get() public method

定義於: yii\base\BaseObject::__get()

回傳物件屬性的值。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $value = $object->property; 時,將會隱式呼叫此方法。

另請參閱 __set()

public mixed __get ( $name )
$name 字串

$name

return mixed

屬性值

throws yii\base\UnknownPropertyException

如果未定義屬性

throws yii\base\InvalidCallException

如果屬性為唯寫

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public method

定義於: yii\base\BaseObject::__isset()

檢查屬性是否已設定,即已定義且非 null。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($object->property) 時,將會隱式呼叫此方法。

請注意,如果未定義屬性,將傳回 false。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.php

public boolean __isset ( $name )
$name 字串

$name

return boolean

具名屬性是否已設定 (非 null)。

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

定義於: yii\base\BaseObject::__set()

設定物件屬性的值。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $object->property = $value; 時,將會隱式呼叫此方法。

另請參閱 __get()

public void __set ( $name, $value )
$name 字串

$name

$value mixed

屬性值

throws yii\base\UnknownPropertyException

如果未定義屬性

throws yii\base\InvalidCallException

如果屬性為唯讀

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__toString() public method

字串魔術方法。

public string __toString ( )
return 字串

DB 表達式。

                public function __toString()
{
    return $this->expression;
}

            
__unset() public method

定義於: yii\base\BaseObject::__unset()

將物件屬性設定為 null。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 unset($object->property) 時,將會隱式呼叫此方法。

請注意,如果未定義屬性,此方法將不會執行任何操作。如果屬性為唯讀,它將拋出例外。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.php

public void __unset ( $name )
$name 字串

$name

throws yii\base\InvalidCallException

如果屬性為唯讀。

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
canGetProperty() public method

定義於: yii\base\BaseObject::canGetProperty()

回傳一個值,指示是否可以讀取屬性。

如果屬性可讀取,則滿足以下條件:

  • 類別具有與指定名稱相關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有帶有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canSetProperty()

public boolean canGetProperty ( $name, $checkVars true )
$name 字串

$name

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可以讀取

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

定義於: yii\base\BaseObject::canSetProperty()

回傳一個值,指示是否可以設定屬性。

如果屬性可寫入,則滿足以下條件:

  • 類別具有與指定名稱相關聯的 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有帶有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canGetProperty()

public boolean canSetProperty ( $name, $checkVars true )
$name 字串

$name

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可以寫入

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
自 2.0.14 版本起已棄用。在 PHP >=5.5 上,請改用 ::class

定義於: yii\base\BaseObject::className()

回傳此類別的完整限定名稱。

public static string className ( )
return 字串

此類別的完整限定名稱。

                public static function className()
{
    return get_called_class();
}

            
hasMethod() public method

定義於: yii\base\BaseObject::hasMethod()

回傳一個值,指示是否已定義方法。

預設實作是對 php 函數 method_exists() 的呼叫。當您實作 php 魔術方法 __call() 時,可以覆寫此方法。

public boolean hasMethod ( $name )
$name 字串

方法名稱

return boolean

方法是否已定義

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

定義於: yii\base\BaseObject::hasProperty()

傳回值,指出是否已定義屬性。

如果滿足以下條件,則定義了屬性:

  • 類別具有與指定名稱相關聯的 getter 或 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有帶有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱

public boolean hasProperty ( $name, $checkVars true )
$name 字串

$name

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否已定義

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

定義於: yii\base\BaseObject::init()

初始化物件。

在物件使用給定組態初始化之後,將在建構函式的結尾調用此方法。

public void init ( )

                public function init()
{
}