function Insert::execute

Same name in this branch
  1. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Insert.php \Drupal\sqlite\Driver\Database\sqlite\Insert::execute()
  2. 11.x core/modules/mysql/src/Driver/Database/mysql/Insert.php \Drupal\mysql\Driver\Database\mysql\Insert::execute()
  3. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Insert.php \Drupal\pgsql\Driver\Database\pgsql\Insert::execute()
Same name in other branches
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Insert.php \Drupal\sqlite\Driver\Database\sqlite\Insert::execute()
  2. 9 core/modules/mysql/src/Driver/Database/mysql/Insert.php \Drupal\mysql\Driver\Database\mysql\Insert::execute()
  3. 9 core/modules/pgsql/src/Driver/Database/pgsql/Insert.php \Drupal\pgsql\Driver\Database\pgsql\Insert::execute()
  4. 9 core/lib/Drupal/Core/Database/Query/Insert.php \Drupal\Core\Database\Query\Insert::execute()
  5. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Insert.php \Drupal\Core\Database\Driver\sqlite\Insert::execute()
  6. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Insert.php \Drupal\Core\Database\Driver\mysql\Insert::execute()
  7. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Insert.php \Drupal\Core\Database\Driver\pgsql\Insert::execute()
  8. 8.9.x core/lib/Drupal/Core/Database/Query/Insert.php \Drupal\Core\Database\Query\Insert::execute()
  9. 10 core/modules/sqlite/src/Driver/Database/sqlite/Insert.php \Drupal\sqlite\Driver\Database\sqlite\Insert::execute()
  10. 10 core/modules/mysql/src/Driver/Database/mysql/Insert.php \Drupal\mysql\Driver\Database\mysql\Insert::execute()
  11. 10 core/modules/pgsql/src/Driver/Database/pgsql/Insert.php \Drupal\pgsql\Driver\Database\pgsql\Insert::execute()
  12. 10 core/lib/Drupal/Core/Database/Query/Insert.php \Drupal\Core\Database\Query\Insert::execute()

Executes the insert query.

Return value

int|null|string 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 Query::execute

3 methods override Insert::execute()
Insert::execute in core/modules/sqlite/src/Driver/Database/sqlite/Insert.php
Executes the insert query.
Insert::execute in core/modules/mysql/src/Driver/Database/mysql/Insert.php
Executes the insert query.
Insert::execute in core/modules/pgsql/src/Driver/Database/pgsql/Insert.php
Executes the insert query.

File

core/lib/Drupal/Core/Database/Query/Insert.php, line 59

Class

Insert
General class for an abstracted INSERT query.

Namespace

Drupal\Core\Database\Query

Code

public function execute() {
    // If validation fails, simply return NULL. Note that validation routines
    // in preExecute() may throw exceptions instead.
    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)) {
        $sql = (string) $this;
        // The SelectQuery may contain arguments, load and pass them through.
        return $this->connection
            ->query($sql, $this->fromQuery
            ->getArguments(), $this->queryOptions);
    }
    $last_insert_id = 0;
    $stmt = $this->connection
        ->prepareStatement((string) $this, $this->queryOptions);
    try {
        // Per https://en.wikipedia.org/wiki/Insert_%28SQL%29#Multirow_inserts,
        // not all databases implement SQL-92's standard syntax for multi-row
        // inserts. Therefore, in the degenerate case, execute a separate query
        // for each row, all within a single transaction for atomicity and
        // performance.
        $transaction = $this->connection
            ->startTransaction();
        foreach ($this->insertValues as $insert_values) {
            $stmt->execute($insert_values, $this->queryOptions);
            $last_insert_id = $this->connection
                ->lastInsertId();
        }
    } catch (\Exception $e) {
        if (isset($transaction)) {
            // One of the INSERTs failed, rollback the whole batch.
            $transaction->rollBack();
        }
        // Rethrow the exception for the calling code.
        throw $e;
    }
    // Re-initialize the values array so that we can re-use this query.
    $this->insertValues = [];
    // Transaction commits here where $transaction looses scope.
    return $last_insert_id;
}

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