Core\Db\Write::lastId
$write->lastId(): ?uintTells you the key the database gave to the row a statement inserted.
Core\Db\Connection::execute returns a Core\Db\Write, and lastId is the key on it. The type is ?uint: a whole number, or null where the statement handed no key back.
This is the number a program needs straight after an insert, because the rows that belong to the new one are written with it: the lines of an order, the tags of an article, the members of a group.
A statement that inserts several rows hands back one key, and it is the last row's. Where your program needs every key, insert the rows one at a time.
The key belongs to the write that produced it. Statements that run later on the same connection do not change what an earlier Core\Db\Write answers, so the key is still the right one after other work has happened.
On PostgreSQL the key comes from a returning clause, because that server has no key of its own to report.
The examples read the key of a new row, ask a statement that inserted nothing for one, and write the lines that belong to an order.
Return value
Returns ?uint. A ?uint: the key this statement generated, or null if it generated none. A statement that inserted no row returns null. PostgreSQL has no last insert id, so the key comes from a RETURNING clause. It is the first column of the last row returned, if that column is declared as an integer. On MySQL it is the AUTO_INCREMENT value the server reports for this write. On SQLite it is the rowid of a row this statement inserted, which may be 0. SQL Server always returns null, because the server does not send a generated key.
Examples
The examples below run with this configuration file.
# The `notes` database is kept in memory. It is deleted when the program ends.[db.notes]driver = "sqlite"path = ":memory:"
# The program may open the `notes` database.[capabilities.db]connect = ["notes"]Read the key of a new row
<?nvs// Shows how to read the key the database gave to a row you just inserted.// `Core\Db\Connection::execute` returns a `Core\Db\Write`, and `lastId` is the// key on it.
var $db = Core\Db::connect('notes');$db->execute('create table notes (id integer primary key, text text not null)', []);
var $write = $db->execute('insert into notes (text) values (?)', ['buy milk']);
// The table was empty, so the new note is note 1. This prints `note 1`.echo 'note ', $write->lastId() ?? 0, "\n";
$db->close();note 1A statement that inserted nothing
<?nvs// Shows what the key is when the statement inserted no row. There is no new// row, so there is no key, and `lastId` is `null`.
var $db = Core\Db::connect('notes');$db->execute('create table notes (id integer primary key, text text not null)', []);$db->execute('insert into notes (text) values (?)', ['buy milk']);
var $write = $db->execute('update notes set text = ? where id = ?', ['buy bread', 1]);
if ($write->lastId() == null) { // The update changed a row and inserted none, so there is no new key to // read. This prints `the update handed back no key`. echo "the update handed back no key\n";}
$db->close();the update handed back no keyWrite the lines that belong to an order
<?nvs// Shows the work a program really does here: an order is written, and the// lines of that order are written with the key the database gave it.
var $db = Core\Db::connect('notes');$db->execute('create table orders (id integer primary key, customer text not null)', []);$db->execute('create table orderLines (id integer primary key, orderId int not null, item text not null)', []);
var $order = $db->execute('insert into orders (customer) values (?)', ['Ana']);?uint $id = $order->lastId();
if ($id != null) { $db->execute( 'insert into orderLines (orderId, item) values (?, ?), (?, ?)', [$id, 'blue mug', $id, 'green mug'], );}
// Both lines carry the key of the new order. This prints `order 1: blue mug`// and `order 1: green mug`.foreach ($db->query('select orderId, item from orderLines order by id', []) as Core\Db\Row $row) { echo 'order ', $row->uint('orderId') ?? 0, ': ', $row->string('item') ?? '', "\n";}
$db->close();order 1: blue mugorder 1: green mug