Class yii\db\ArrayExpression
繼承 | yii\db\ArrayExpression |
---|---|
實作 | ArrayAccess, Countable, IteratorAggregate, yii\db\ExpressionInterface |
自版本起可用 | 2.0.14 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/db/ArrayExpression.php |
ArrayExpression 類別代表一個陣列 SQL 表達式。
此類型的表達式也可以在條件中使用
$query->andWhere(['@>', 'items', new ArrayExpression([1, 2, 3], 'integer')])
根據 DBMS 的不同,這將產生一個準備完善的條件。例如,在 PostgreSQL 中,它將被編譯為 WHERE "items" @> ARRAY[1, 2, 3]::integer[]
。
公共方法
方法 | 描述 | 定義於 |
---|---|---|
__construct() | ArrayExpression 建構子。 | yii\db\ArrayExpression |
count() | 計算物件的元素 | yii\db\ArrayExpression |
getDimension() | yii\db\ArrayExpression | |
getIterator() | 檢索外部迭代器 | yii\db\ArrayExpression |
getType() | yii\db\ArrayExpression | |
getValue() | yii\db\ArrayExpression | |
offsetExists() | 偏移量是否存在 | yii\db\ArrayExpression |
offsetGet() | 要檢索的偏移量 | yii\db\ArrayExpression |
offsetSet() | 要設定的偏移量 | yii\db\ArrayExpression |
offsetUnset() | 要取消設定的偏移量 | yii\db\ArrayExpression |
方法詳情
ArrayExpression 建構子。
public void __construct ( $value, $type = null, $dimension = 1 ) | ||
$value | array|yii\db\QueryInterface|mixed |
陣列內容。可以表示為值陣列或傳回這些值的查詢。單一值將被視為包含一個元素的陣列。 |
$type | string|null |
陣列元素的類型。預設為 |
$dimension | integer |
選擇元素所需的索引數量 |
public function __construct($value, $type = null, $dimension = 1)
{
if ($value instanceof self) {
$value = $value->getValue();
}
$this->value = $value;
$this->type = $type;
$this->dimension = $dimension;
}
計算物件的元素
public integer count ( ) | ||
return | integer |
自訂計數作為整數。 傳回值會被轉換為整數。 |
---|
#[\ReturnTypeWillChange]
public function count()
{
return count($this->value);
}
public integer getDimension ( ) | ||
return | integer |
選擇元素所需的索引數量 |
---|
public function getDimension()
{
return $this->dimension;
}
檢索外部迭代器
public Traversable getIterator ( ) | ||
return | Traversable |
實作 Iterator 或 Traversable 的物件實例 |
---|---|---|
throws | yii\base\InvalidConfigException |
當 ArrayExpression 包含 QueryInterface 物件時 |
#[\ReturnTypeWillChange]
public function getIterator()
{
$value = $this->getValue();
if ($value instanceof QueryInterface) {
throw new InvalidConfigException('The ArrayExpression class can not be iterated when the value is a QueryInterface object');
}
if ($value === null) {
$value = [];
}
return new \ArrayIterator($value);
}
public array|mixed|yii\db\QueryInterface getValue ( ) |
public function getValue()
{
return $this->value;
}
偏移量是否存在
public boolean offsetExists ( $offset ) | ||
$offset | mixed |
要檢查的偏移量。 |
return | boolean |
成功時為 True,失敗時為 false。 如果傳回非布林值,則傳回值將被轉換為布林值。 |
---|
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->value[$offset]);
}
要檢索的偏移量
public mixed offsetGet ( $offset ) | ||
$offset | mixed |
要檢索的偏移量。 |
return | mixed |
可以傳回所有值類型。 |
---|
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->value[$offset];
}
要設定的偏移量
public void offsetSet ( $offset, $value ) | ||
$offset | mixed |
要將值指派到的偏移量。 |
$value | mixed |
要設定的值。 |
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->value[$offset] = $value;
}
要取消設定的偏移量
public void offsetUnset ( $offset ) | ||
$offset | mixed |
要取消設定的偏移量。 |
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->value[$offset]);
}
註冊 或 登入 以發表評論。