function 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.
File
-
core/
lib/ Drupal/ Core/ Database/ Connection.php, line 842
Class
- Connection
- Base Database API class.
Namespace
Drupal\Core\DatabaseCode
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;
}