Skip to content

Commit

Permalink
[BUGFIX] Cast return value of postProcessDatabaseInsert to integer
Browse files Browse the repository at this point in the history
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 <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
  • Loading branch information
susannemoog authored and lolli42 committed Mar 15, 2018
1 parent 8fa9956 commit 7603c0f
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion typo3/sysext/core/Classes/DataHandling/DataHandler.php
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 7603c0f

Please sign in to comment.