Throw exception when last insert ID can't be determined

This commit is contained in:
Alex Cabal 2024-05-13 21:46:29 -05:00
parent e5973c8938
commit ace9cea6b7

View file

@ -400,21 +400,24 @@ class DbConnection{
} }
} }
public function GetLastInsertedId(): ?int{ /**
$id = $this->_link->lastInsertId(); * Get the ID of the last row that was inserted during this database connection.
* @throws Exceptions\DatabaseQueryException When the last inserted ID can't be determined.
*/
public function GetLastInsertedId(): int{
try{
$id = $this->_link->lastInsertId();
}
catch(\PDOException){
$id = false;
}
if($id === false){ if($id === false || $id == '0'){
return null; throw new Exceptions\DatabaseQueryException('Couldn\'t get last insert ID.');
} }
else{ else{
$id = (int)$id; return intval($id);
} }
if($id == 0){
return null;
}
return $id;
} }
/** /**