Class yii\db\ArrayExpression
Inheritance | yii\db\ArrayExpression |
---|---|
Implements | ArrayAccess, Countable, IteratorAggregate, yii\db\ExpressionInterface |
Available since version | 2.0.14 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/db/ArrayExpression.php |
Class ArrayExpression represents an array SQL expression.
Expressions of this type can be used in conditions as well:
$query->andWhere(['@>', 'items', new ArrayExpression([1, 2, 3], 'integer')])
which, depending on DBMS, will result in a well-prepared condition. For example, in
PostgreSQL it will be compiled to WHERE "items" @> ARRAY[1, 2, 3]::integer[]
.
Public Methods
Method | Description | Defined By |
---|---|---|
__construct() | ArrayExpression constructor. | yii\db\ArrayExpression |
count() | Count elements of an object | yii\db\ArrayExpression |
getDimension() | yii\db\ArrayExpression | |
getIterator() | Retrieve an external iterator | yii\db\ArrayExpression |
getType() | yii\db\ArrayExpression | |
getValue() | yii\db\ArrayExpression | |
offsetExists() | Whether a offset exists | yii\db\ArrayExpression |
offsetGet() | Offset to retrieve | yii\db\ArrayExpression |
offsetSet() | Offset to set | yii\db\ArrayExpression |
offsetUnset() | Offset to unset | yii\db\ArrayExpression |
Method Details
ArrayExpression constructor.
public void __construct ( $value, $type = null, $dimension = 1 ) | ||
$value | array|yii\db\QueryInterface|mixed |
The array content. Either represented as an array of values or a Query that returns these values. A single value will be considered as an array containing one element. |
$type | string|null |
The type of the array elements. Defaults to |
$dimension | integer |
The number of indices needed to select an element |
public function __construct($value, $type = null, $dimension = 1)
{
if ($value instanceof self) {
$value = $value->getValue();
}
$this->value = $value;
$this->type = $type;
$this->dimension = $dimension;
}
Count elements of an object
public integer count ( ) | ||
return | integer |
The custom count as an integer. The return value is cast to an integer. |
---|
#[\ReturnTypeWillChange]
public function count()
{
return count($this->value);
}
public integer getDimension ( ) | ||
return | integer |
The number of indices needed to select an element |
---|
public function getDimension()
{
return $this->dimension;
}
Retrieve an external iterator
public Traversable getIterator ( ) | ||
return | Traversable |
An instance of an object implementing Iterator or Traversable |
---|---|---|
throws | yii\base\InvalidConfigException |
when ArrayExpression contains QueryInterface object |
#[\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;
}
Whether a offset exists
public boolean offsetExists ( $offset ) | ||
$offset | mixed |
An offset to check for. |
return | boolean |
True on success or false on failure. The return value will be casted to boolean if non-boolean was returned. |
---|
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->value[$offset]);
}
Offset to retrieve
public mixed offsetGet ( $offset ) | ||
$offset | mixed |
The offset to retrieve. |
return | mixed |
Can return all value types. |
---|
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->value[$offset];
}
Offset to set
public void offsetSet ( $offset, $value ) | ||
$offset | mixed |
The offset to assign the value to. |
$value | mixed |
The value to set. |
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->value[$offset] = $value;
}
Offset to unset
public void offsetUnset ( $offset ) | ||
$offset | mixed |
The offset to unset. |
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->value[$offset]);
}
Signup or Login in order to comment.