diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2015-12-08 09:40:20 +0100 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2015-12-08 09:40:20 +0100 |
commit | f2c7acb3c065c35a1e75d512d0ce193f1989296f (patch) | |
tree | 4c3cec0c14b0ea0b1f0cb092515d112cafd13ec9 /lib/private/db | |
parent | 736e133c047a22b587294a1231b61ac2575f3f83 (diff) | |
download | nextcloud-server-f2c7acb3c065c35a1e75d512d0ce193f1989296f.tar.gz nextcloud-server-f2c7acb3c065c35a1e75d512d0ce193f1989296f.zip |
Allow getting the last insert id without much hassle
Diffstat (limited to 'lib/private/db')
-rw-r--r-- | lib/private/db/querybuilder/querybuilder.php | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/private/db/querybuilder/querybuilder.php b/lib/private/db/querybuilder/querybuilder.php index 02d8ee4344d..e70733b5509 100644 --- a/lib/private/db/querybuilder/querybuilder.php +++ b/lib/private/db/querybuilder/querybuilder.php @@ -1024,6 +1024,21 @@ class QueryBuilder implements IQueryBuilder { } /** + * Used to get the id of the last inserted element + * @return int + * @throws \BadMethodCallException When being called before an insert query has been run. + */ + public function getLastInsertId() { + $from = $this->getQueryPart('from'); + + if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && !empty($from)) { + return (int) $this->connection->lastInsertId($from['table']); + } + + throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); + } + + /** * @param string $table * @return string */ |