From f2c7acb3c065c35a1e75d512d0ce193f1989296f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 8 Dec 2015 09:40:20 +0100 Subject: [PATCH] Allow getting the last insert id without much hassle --- lib/private/db/querybuilder/querybuilder.php | 15 +++++++++++ lib/public/db/querybuilder/iquerybuilder.php | 8 ++++++ .../lib/db/querybuilder/querybuildertest.php | 25 +++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/lib/private/db/querybuilder/querybuilder.php b/lib/private/db/querybuilder/querybuilder.php index 02d8ee4344..e70733b550 100644 --- a/lib/private/db/querybuilder/querybuilder.php +++ b/lib/private/db/querybuilder/querybuilder.php @@ -1023,6 +1023,21 @@ class QueryBuilder implements IQueryBuilder { return new QueryFunction($call); } + /** + * 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 diff --git a/lib/public/db/querybuilder/iquerybuilder.php b/lib/public/db/querybuilder/iquerybuilder.php index beb922b7fe..e3105cf134 100644 --- a/lib/public/db/querybuilder/iquerybuilder.php +++ b/lib/public/db/querybuilder/iquerybuilder.php @@ -796,4 +796,12 @@ interface IQueryBuilder { * @since 8.2.0 */ public function createFunction($call); + + /** + * Used to get the id of the last inserted element + * @return int + * @throws \BadMethodCallException When being called before an insert query has been run. + * @since 9.0.0 + */ + public function getLastInsertId(); } diff --git a/tests/lib/db/querybuilder/querybuildertest.php b/tests/lib/db/querybuilder/querybuildertest.php index ca3901ad04..828a860ee8 100644 --- a/tests/lib/db/querybuilder/querybuildertest.php +++ b/tests/lib/db/querybuilder/querybuildertest.php @@ -1086,6 +1086,31 @@ class QueryBuilderTest extends \Test\TestCase { ); } + public function testGetLastInsertId() { + $qB = $this->connection->getQueryBuilder(); + + try { + $qB->getLastInsertId(); + $this->fail('getLastInsertId() should throw an exception, when being called before insert()'); + } catch (\BadMethodCallException $e) { + $this->assertTrue(true); + } + + $qB->insert('appconfig') + ->values([ + 'appid' => $qB->expr()->literal('testFirstResult'), + 'configkey' => $qB->expr()->literal('testing' . 50), + 'configvalue' => $qB->expr()->literal(100 - 50), + ]) + ->execute(); + + $actual = $qB->getLastInsertId(); + + $this->assertNotNull($actual); + $this->assertInternalType('int', $actual); + $this->assertEquals($this->connection->lastInsertId('*PREFIX*appconfig'), $actual); + } + public function dataGetTableName() { return [ ['*PREFIX*table', null, '`*PREFIX*table`'],