function Connection::lastInsertId

Same name in other branches
  1. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::lastInsertId()
  2. 11.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::lastInsertId()

Returns the ID of the last inserted row or sequence value.

This method should normally be used only within database driver code.

This is a proxy to invoke lastInsertId() from the wrapped connection. If a sequence name is not specified for the name parameter, this returns a string representing the row ID of the last row that was inserted into the database. If a sequence name is specified for the name parameter, this returns a string representing the last value retrieved from the specified sequence object.

Parameters

string|null $name: (Optional) Name of the sequence object from which the ID should be returned.

Return value

string The value returned by the wrapped connection.

Throws

\Drupal\Core\Database\DatabaseExceptionWrapper In case of failure.

1 call to Connection::lastInsertId()
Connection::query in core/lib/Drupal/Core/Database/Connection.php
Executes a query string against the database.

File

core/lib/Drupal/Core/Database/Connection.php, line 1123

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

public function lastInsertId(?string $name = NULL) : string {
    if (($last_insert_id = $this->connection
        ->lastInsertId($name)) === FALSE) {
        throw new DatabaseExceptionWrapper("Could not determine last insert id" . $name === NULL ? '' : " for sequence {$name}");
    }
    return $last_insert_id;
}

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