Skip to content

Commit 6a73754

Browse files
committed
Connection: Add lastInsertId to the interface
Helps with static analysis and test doubles. (phpunit will not mock/stub this method unless declared)
1 parent 3f578b1 commit 6a73754

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/Connection.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use ipl\Sql\Contract\Adapter;
99
use ipl\Sql\Contract\Quoter;
1010
use ipl\Stdlib\Plugins;
11+
use LogicException;
1112
use PDO;
1213
use PDOStatement;
1314

@@ -438,6 +439,25 @@ public function insert($table, $data)
438439
return $this->prepexec($insert);
439440
}
440441

442+
/**
443+
* Get the ID of the last inserted row
444+
*
445+
* @param ?string $name The name of the sequence object from which the ID should be returned.
446+
*
447+
* @throws LogicException If no connection to the database is established
448+
* @return false|string
449+
*/
450+
public function lastInsertId(?string $name = null): false|string
451+
{
452+
if ($this->pdo === null) {
453+
throw new LogicException(
454+
'Cannot get last insert ID because no connection to the database is established.'
455+
);
456+
}
457+
458+
return $this->pdo->lastInsertId($name);
459+
}
460+
441461
/**
442462
* Update table rows with the specified data, optionally based on a given condition
443463
*

0 commit comments

Comments
 (0)