You are here

public function Insert::execute in Drupal 8

Same name in this branch
  1. 8 core/lib/Drupal/Core/Database/Query/Insert.php \Drupal\Core\Database\Query\Insert::execute()
  2. 8 core/lib/Drupal/Core/Database/Driver/sqlite/Insert.php \Drupal\Core\Database\Driver\sqlite\Insert::execute()
  3. 8 core/lib/Drupal/Core/Database/Driver/pgsql/Insert.php \Drupal\Core\Database\Driver\pgsql\Insert::execute()
  4. 8 core/lib/Drupal/Core/Database/Driver/mysql/Insert.php \Drupal\Core\Database\Driver\mysql\Insert::execute()
Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Driver/mysql/Insert.php \Drupal\Core\Database\Driver\mysql\Insert::execute()

Executes the insert query.

Return value

The last insert ID of the query, if one exists. If the query was given multiple sets of values to insert, the return value is undefined. If no fields are specified, this method will do nothing and return NULL. That That makes it safe to use in multi-insert loops.

Overrides Insert::execute

File

core/lib/Drupal/Core/Database/Driver/mysql/Insert.php, line 12

Class

Insert
MySQL implementation of \Drupal\Core\Database\Query\Insert.

Namespace

Drupal\Core\Database\Driver\mysql

Code

public function execute() {
  if (!$this
    ->preExecute()) {
    return NULL;
  }

  // If we're selecting from a SelectQuery, finish building the query and
  // pass it back, as any remaining options are irrelevant.
  if (empty($this->fromQuery)) {
    $max_placeholder = 0;
    $values = [];
    foreach ($this->insertValues as $insert_values) {
      foreach ($insert_values as $value) {
        $values[':db_insert_placeholder_' . $max_placeholder++] = $value;
      }
    }
  }
  else {
    $values = $this->fromQuery
      ->getArguments();
  }
  $last_insert_id = $this->connection
    ->query((string) $this, $values, $this->queryOptions);

  // Re-initialize the values array so that we can re-use this query.
  $this->insertValues = [];
  return $last_insert_id;
}