From 7603c0f2e5ae4c6d6e4befe2ef0763bdd503a427 Mon Sep 17 00:00:00 2001 From: Susanne Moog Date: Thu, 15 Mar 2018 15:03:13 +0100 Subject: [PATCH] [BUGFIX] Cast return value of postProcessDatabaseInsert to integer Connection::lastInsertId returns a string but DataHandler::postProcessDatabaseInsert has a strict integer return value, so the value has to be casted to integer on return. Additionally, when using sqlserver doctrine fails to fetch the last inserted id under certain circumstances. An additional retrieval method was introduced to mitigate that error. Resolves: #84219 Releases: master, 8.7 Change-Id: I94dc0dc964aef26380703f641691c6a80ec5180d Reviewed-on: https://review.typo3.org/56118 Reviewed-by: Frank Naegler Tested-by: Frank Naegler Tested-by: TYPO3com Reviewed-by: Susanne Moog Tested-by: Susanne Moog Reviewed-by: Christian Kuhn Tested-by: Christian Kuhn --- .../core/Classes/DataHandling/DataHandler.php | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/typo3/sysext/core/Classes/DataHandling/DataHandler.php b/typo3/sysext/core/Classes/DataHandling/DataHandler.php index ddd8640f327a..d61f851811d5 100644 --- a/typo3/sysext/core/Classes/DataHandling/DataHandler.php +++ b/typo3/sysext/core/Classes/DataHandling/DataHandler.php @@ -9123,8 +9123,38 @@ protected function postProcessDatabaseInsert(Connection $connection, string $tab // Return the actual ID we forced on insert as a surrogate. return $suggestedUid; } + if ($connection->getDatabasePlatform() instanceof SQLServerPlatform) { + return $this->postProcessSqlServerInsert($connection, $tableName); + } + $id = $connection->lastInsertId($tableName); + return (int)$id; + } - return $connection->lastInsertId($tableName); + /** + * Get the last insert ID from sql server + * + * - first checks whether doctrine might be able to fetch the ID from the + * sequence table + * - if that does not succeed it manually selects the current IDENTITY value + * from a table + * - returns 0 if both fail + * + * @param \TYPO3\CMS\Core\Database\Connection $connection + * @param string $tableName + * @return int + * @throws \Doctrine\DBAL\DBALException + */ + protected function postProcessSqlServerInsert(Connection $connection, string $tableName): int + { + $id = $connection->lastInsertId($tableName); + if (!((int)$id > 0)) { + $table = $connection->quoteIdentifier($tableName); + $result = $connection->executeQuery('SELECT IDENT_CURRENT(\'' . $table . '\') AS id')->fetch(); + if (isset($result['id']) && $result['id'] > 0) { + $id = $result['id']; + } + } + return (int)$id; } /**