[BUGFIX] DBAL: Fix retrieving the last insert id 87/43187/2
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Fri, 11 Sep 2015 08:06:16 +0000 (10:06 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Fri, 11 Sep 2015 09:38:15 +0000 (11:38 +0200)
Only retrieve the last insert id if the table actually has an auto
incrementing field, otherwise trying to retrieve the last inserted
id can result in errors and transaction rollbacks.

The last inserted id is also retrieved on plain INSERT statements, not
only for INSERT statements that require a subsequent UPDATE to store
BLOB/CLOB values.

Resolves: #67077
Releases: master
Change-Id: I87b62cb004a3f9356d47e18ad4dd5fb78c7c71fa
Reviewed-on: http://review.typo3.org/43187
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/dbal/Classes/Database/DatabaseConnection.php

index 224de91..4fb6db5 100644 (file)
@@ -522,11 +522,18 @@ class DatabaseConnection extends \TYPO3\CMS\Core\Database\DatabaseConnection {
                                $this->lastQuery = $this->INSERTquery($table, $fields_values, $no_quote_fields);
                                if (is_string($this->lastQuery)) {
                                        $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery, FALSE);
+                                       if ($this->handlerInstance[$this->lastHandlerKey]->hasInsertID && !empty($this->cache_autoIncFields[$table])) {
+                                               // The table is able to retrieve the ID of the last insert, use it to update the blob below
+                                               $new_id = $this->handlerInstance[$this->lastHandlerKey]->Insert_ID($table, $this->cache_autoIncFields[$table]);
+                                               if ($table !== 'tx_dbal_debuglog') {
+                                                       $this->handlerInstance[$this->lastHandlerKey]->last_insert_id = $new_id;
+                                               }
+                                       }
                                } else {
                                        $this->handlerInstance[$this->lastHandlerKey]->StartTrans();
                                        if ((string)$this->lastQuery[0] !== '') {
                                                $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery[0], FALSE);
-                                               if ($this->handlerInstance[$this->lastHandlerKey]->hasInsertID) {
+                                               if ($this->handlerInstance[$this->lastHandlerKey]->hasInsertID && !empty($this->cache_autoIncFields[$table])) {
                                                        // The table is able to retrieve the ID of the last insert, use it to update the blob below
                                                        $new_id = $this->handlerInstance[$this->lastHandlerKey]->Insert_ID($table, $this->cache_autoIncFields[$table]);
                                                        if ($table !== 'tx_dbal_debuglog') {