# Class yii\db\pgsql\ArrayExpressionBuilder

Inheritance | yii\db\pgsql\ArrayExpressionBuilder
Implements | yii\db\ExpressionBuilderInterface
Uses Traits | yii\db\ExpressionBuilderTrait
Available since version | 2.0.14
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/db/pgsql/ArrayExpressionBuilder.php

Class ArrayExpressionBuilder builds yii\db\ArrayExpression for PostgreSQL DBMS.

## Protected Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
__construct() | ExpressionBuilderTrait constructor. | yii\db\ExpressionBuilderTrait
build() | Method builds the raw SQL from the $expression that will not be additionally escaped or quoted. | yii\db\pgsql\ArrayExpressionBuilder

## Protected Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
buildPlaceholders() | Builds placeholders array out of $expression values | yii\db\pgsql\ArrayExpressionBuilder
buildSubqueryArray() | Build an array expression from a subquery SQL. | yii\db\pgsql\ArrayExpressionBuilder
getTypehint() |  | yii\db\pgsql\ArrayExpressionBuilder
typecastValue() | Casts $value to use in $expression | yii\db\pgsql\ArrayExpressionBuilder

## Method Details

Hide inherited methods

__construct() public method

Defined in: yii\db\ExpressionBuilderTrait::__construct()

ExpressionBuilderTrait constructor.

public void __construct ( yii\db\QueryBuilder $queryBuilder )
$queryBuilder | yii\db\QueryBuilder |

Source code

                    public function __construct(QueryBuilder $queryBuilder)
    {
        $this->queryBuilder = $queryBuilder;
    }

build() public method

Method builds the raw SQL from the $expression that will not be additionally escaped or quoted.

public string build ( yii\db\ExpressionInterface $expression, array &$params = [] )
$expression | yii\db\ArrayExpression|yii\db\ExpressionInterface | The expression to be built
$params | array | The binding parameters.
return | string | The raw SQL that will not be additionally escaped or quoted.

Source code

                    public function build(ExpressionInterface $expression, array &$params = [])
    {
        $value = $expression->getValue();
        if ($value === null) {
            return 'NULL';
        }
        if ($value instanceof Query) {
            list ($sql, $params) = $this->queryBuilder->build($value, $params);
            return $this->buildSubqueryArray($sql, $expression);
        }
        $placeholders = $this->buildPlaceholders($expression, $params);
        return 'ARRAY[' . implode(', ', $placeholders) . ']' . $this->getTypehint($expression);
    }

buildPlaceholders() protected method

Builds placeholders array out of $expression values

protected array buildPlaceholders ( yii\db\ExpressionInterface $expression, &$params )
$expression | yii\db\ExpressionInterface|yii\db\ArrayExpression |
$params | array | The binding parameters.

Source code

                    protected function buildPlaceholders(ExpressionInterface $expression, &$params)
    {
        $value = $expression->getValue();
        $placeholders = [];
        if ($value === null || !is_array($value) && !$value instanceof \Traversable) {
            return $placeholders;
        }
        if ($expression->getDimension() > 1) {
            foreach ($value as $item) {
                $placeholders[] = $this->build($this->unnestArrayExpression($expression, $item), $params);
            }
            return $placeholders;
        }
        foreach ($value as $item) {
            if ($item instanceof Query) {
                list ($sql, $params) = $this->queryBuilder->build($item, $params);
                $placeholders[] = $this->buildSubqueryArray($sql, $expression);
                continue;
            }
            $item = $this->typecastValue($expression, $item);
            if ($item instanceof ExpressionInterface) {
                $placeholders[] = $this->queryBuilder->buildExpression($item, $params);
                continue;
            }
            $placeholders[] = $this->queryBuilder->bindParam($item, $params);
        }
        return $placeholders;
    }

buildSubqueryArray() protected method

Build an array expression from a subquery SQL.

protected string buildSubqueryArray ( $sql, yii\db\ArrayExpression $expression )
$sql | string | The subquery SQL.
$expression | yii\db\ArrayExpression |
return | string | The subquery array expression.

Source code

                    protected function buildSubqueryArray($sql, ArrayExpression $expression)
    {
        return 'ARRAY(' . $sql . ')' . $this->getTypehint($expression);
    }

getTypehint() protected method

protected string getTypehint ( yii\db\ArrayExpression $expression )
$expression | yii\db\ArrayExpression |
return | string | The typecast expression based on type.

Source code

                    protected function getTypehint(ArrayExpression $expression)
    {
        if ($expression->getType() === null) {
            return '';
        }
        $result = '::' . $expression->getType();
        $result .= str_repeat('[]', $expression->getDimension());
        return $result;
    }

typecastValue() protected method

Casts $value to use in $expression

protected yii\db\JsonExpression typecastValue ( yii\db\ArrayExpression $expression, $value )
$expression | yii\db\ArrayExpression |
$value | mixed |

Source code

                    protected function typecastValue(ArrayExpression $expression, $value)
    {
        if ($value instanceof ExpressionInterface) {
            return $value;
        }
        if (in_array($expression->getType(), [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
            return new JsonExpression($value);
        }
        return $value;
    }