protected function compileInsertFromValues($table, string $tableAlias = null, array $values): array
    {
        if (empty($values)) {
            return [];
        }

        $bindings = [];

        // Step 1. Fetch unique columns list.
        $columns = [];
        foreach ($values as $row) {
            foreach ($row as $column => $value) {
                if (!isset($columns[$column])) {
                    $columns[$column] = count($columns);
                }
            }
        }

        // Step 2. Build the values matrix.
        $compiledRows = [];
        foreach ($values as $row) {
            $compiledRow = [];
            foreach ($columns as $column => $columnIndex) {
                if (array_key_exists($column, $row)) {
                    $compiledRow[$columnIndex] = $this->compileValue($row[$column], $bindings);
                } else {
                    $compiledRow[$columnIndex] = 'DEFAULT';
                }
            }
            $compiledRows[] = '('.implode(', ', $compiledRow).')';
        }

        // Step 3. Build the SQL
        $sqlLine1 = 'INSERT INTO '.$this->compileIdentifierWithAlias($table, $tableAlias, $bindings)
            . ' ('.implode(', ', array_map([$this, 'quoteCompositeIdentifier'], array_keys($columns))).')';
        $sqlLine2 = 'VALUES '.implode(', ', $compiledRows);

        return [new Raw($this->implodeSQL([$sqlLine1, $sqlLine2]), $bindings)];
    }
protected function compileInsertFromValues($table, string $tableAlias = null, array $values): array
    {
        if (empty($values)) {
            return [];
        }

        $bindings = [];

        // Step 1. Fetch unique columns list.
        $columns = [];
        foreach ($values as $row) {
            foreach ($row as $column => $value) {
                if (!isset($columns[$column])) {
                    $columns[$column] = count($columns);
                }
            }
        }

        // Step 2. Build the values matrix.
        $compiledRows = [];
        foreach ($values as $row) {
            $compiledRow = [];
            foreach ($columns as $column => $columnIndex) {
                if (array_key_exists($column, $row)) {
                    $compiledRow[$columnIndex] = $this->compileValue($row[$column], $bindings);
                } else {
                    $compiledRow[$columnIndex] = 'DEFAULT';
                }
            }
            $compiledRows[] = '('.implode(', ', $compiledRow).')';
        }

        // Step 3. Build the SQL
        $sqlLine1 = 'INSERT INTO '.$this->compileIdentifierWithAlias($table, $tableAlias, $bindings)
            . ' ('.implode(', ', array_map([$this, 'quoteCompositeIdentifier'], array_keys($columns))).')';
        $sqlLine2 = 'VALUES '.implode(', ', $compiledRows);

        return [new Raw($this->implodeSQL([$sqlLine1, $sqlLine2]), $bindings)];
    }
protected function compileInsertFromValues ( $ table , string $ tableAlias = null , array $ values ) : array { if ( empty ( $ values ) ) { return [ ] ; } $ bindings = [ ] ; // Step 1. Fetch unique columns list. $ columns = [ ] ; foreach ( $ values as $ row ) { foreach ( $ row as $ column => $ value ) { if ( ! isset ( $ columns [ $ column ] ) ) { $ columns [ $ column ] = count ( $ columns ) ; } } } // Step 2. Build the values matrix. $ compiledRows = [ ] ; foreach ( $ values as $ row ) { $ compiledRow = [ ] ; foreach ( $ columns as $ column => $ columnIndex ) { if ( array_key_exists ( $ column , $ row ) ) { $ compiledRow [ $ columnIndex ] = $ this -> compileValue ( $ row [ $ column ] , $ bindings ) ; } else { $ compiledRow [ $ columnIndex ] = 'DEFAULT' ; } } $ compiledRows [ ] = '(' . implode ( ', ' , $ compiledRow ) . ')' ; } // Step 3. Build the SQL $ sqlLine1 = 'INSERT INTO ' . $ this -> compileIdentifierWithAlias ( $ table , $ tableAlias , $ bindings ) . ' (' . implode ( ', ' , array_map ( [ $ this , 'quoteCompositeIdentifier' ] , array_keys ( $ columns ) ) ) . ')' ; $ sqlLine2 = 'VALUES ' . implode ( ', ' , $ compiledRows ) ; return [ new Raw ( $ this -> implodeSQL ( [ $ sqlLine1 , $ sqlLine2 ] ) , $ bindings ) ] ; }
Compiles set of full INSERT queries from an array of values.

@param string|Query|StatementInterface $table Target table name
@param string|null $tableAlias Table alias
@param mixed[][]|Query[][]|StatementInterface[][] $values Array of rows of values
@return StatementInterface[]
@throws InvalidQueryException
Compiles set of full INSERT queries from an array of values .