function InsertTrait::getInsertPlaceholderFragment

Same name in other branches
  1. 9 core/lib/Drupal/Core/Database/Query/InsertTrait.php \Drupal\Core\Database\Query\InsertTrait::getInsertPlaceholderFragment()
  2. 8.9.x core/lib/Drupal/Core/Database/Query/InsertTrait.php \Drupal\Core\Database\Query\InsertTrait::getInsertPlaceholderFragment()
  3. 11.x core/lib/Drupal/Core/Database/Query/InsertTrait.php \Drupal\Core\Database\Query\InsertTrait::getInsertPlaceholderFragment()

Returns the query placeholders for values that will be inserted.

Parameters

array $nested_insert_values: A nested array of values to insert.

array $default_fields: An array of fields that should be set to their database-defined defaults.

Return value

array An array of insert placeholders.

5 calls to InsertTrait::getInsertPlaceholderFragment()
Insert::__toString in core/modules/mysql/src/Driver/Database/mysql/Insert.php
Implements PHP magic __toString method to convert the query to a string.
Insert::__toString in core/modules/pgsql/src/Driver/Database/pgsql/Insert.php
Implements PHP magic __toString method to convert the query to a string.
Upsert::__toString in core/modules/sqlite/src/Driver/Database/sqlite/Upsert.php
Implements PHP magic __toString method to convert the query to a string.
Upsert::__toString in core/modules/mysql/src/Driver/Database/mysql/Upsert.php
Implements PHP magic __toString method to convert the query to a string.
Upsert::__toString in core/modules/pgsql/src/Driver/Database/pgsql/Upsert.php
Implements PHP magic __toString method to convert the query to a string.

File

core/lib/Drupal/Core/Database/Query/InsertTrait.php, line 151

Class

InsertTrait
Provides common functionality for INSERT and UPSERT queries.

Namespace

Drupal\Core\Database\Query

Code

protected function getInsertPlaceholderFragment(array $nested_insert_values, array $default_fields) {
    $max_placeholder = 0;
    $values = [];
    if ($nested_insert_values) {
        foreach ($nested_insert_values as $insert_values) {
            $placeholders = [];
            // Default fields aren't really placeholders, but this is the most convenient
            // way to handle them.
            $placeholders = array_pad($placeholders, count($default_fields), 'default');
            $new_placeholder = $max_placeholder + count($insert_values);
            for ($i = $max_placeholder; $i < $new_placeholder; ++$i) {
                $placeholders[] = ':db_insert_placeholder_' . $i;
            }
            $max_placeholder = $new_placeholder;
            $values[] = '(' . implode(', ', $placeholders) . ')';
        }
    }
    else {
        // If there are no values, then this is a default-only query. We still need to handle that.
        $placeholders = array_fill(0, count($default_fields), 'default');
        $values[] = '(' . implode(', ', $placeholders) . ')';
    }
    return $values;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.