Issue Bug Closed

#109127DBAL: No identity value was generated by the last statement when using save to database finisher in form

Bernhard Eckl 03/02/26, 14:00 updated 03/31/26, 08:09

Description

I have a form with save to database finisher. Yaml is:

finishers:
  -
    options:
      -
        table: tx_news_domain_model_news
        mode: insert
        databaseColumnMappings:
          pid:
            value: 3323
          is_event:
            value: 1
          hidden:
            value: 1
          author:
            value: '{title} {firstname} {lastname}'
          tstamp:
            value: '{__currentTimestamp}'
          crdate:
            value: '{__currentTimestamp}'
        elements:
          email:
            mapOnDatabaseColumn: author_email
          media:
            mapOnDatabaseColumn: fal_media
            skipIfValueIsEmpty: true
          media-2:
            mapOnDatabaseColumn: fal_media
            skipIfValueIsEmpty: true
          files:
            mapOnDatabaseColumn: fal_related_files
            skipIfValueIsEmpty: true
          heading:
            mapOnDatabaseColumn: title
          subtitle:
            mapOnDatabaseColumn: subtitle
            skipIfValueIsEmpty: true
          teaser:
            mapOnDatabaseColumn: teaser
            skipIfValueIsEmpty: true
          bodytext:
            mapOnDatabaseColumn: bodytext
          eventorganizer:
            mapOnDatabaseColumn: organizer
            skipIfValueIsEmpty: true
          organizersimple:
            mapOnDatabaseColumn: organizer_simple
            skipIfValueIsEmpty: true
          eventlocation:
            mapOnDatabaseColumn: location
            skipIfValueIsEmpty: true
          locationsimple:
            mapOnDatabaseColumn: location_simple
            skipIfValueIsEmpty: true
          eventstart:
            mapOnDatabaseColumn: datetime
            dateFormat: U
          eventend:
            mapOnDatabaseColumn: event_end
            dateFormat: U
          fullday:
            mapOnDatabaseColumn: full_day
            skipIfValueIsEmpty: true
          comment:
            mapOnDatabaseColumn: notes
            skipIfValueIsEmpty: true
          keywords:
            mapOnDatabaseColumn: keywords
            skipIfValueIsEmpty: true
      -
        table: tx_news_domain_model_news
        mode: update
        elements:
          eventstart:
            mapOnDatabaseColumn: event_start
            dateFormat: U
        whereClause:
          uid: '{SaveToDatabase.insertedUids.0}'
      -
        table: sys_file_reference
        mode: insert
        elements:
          media:
            mapOnDatabaseColumn: uid_local
            skipIfValueIsEmpty: true
        databaseColumnMappings:
          tablenames:
            value: tx_news_domain_model_news
          fieldname:
            value: fal_media
          tstamp:
            value: '{__currentTimestamp}'
          crdate:
            value: '{__currentTimestamp}'
          uid_foreign:
            value: '{SaveToDatabase.insertedUids.0}'
          showinpreview:
            value: '1'
      -
        table: sys_file_reference
        mode: insert
        elements:
          media-2:
            mapOnDatabaseColumn: uid_local
            skipIfValueIsEmpty: true
        databaseColumnMappings:
          tablenames:
            value: tx_news_domain_model_news
          fieldname:
            value: fal_media
          tstamp:
            value: '{__currentTimestamp}'
          crdate:
            value: '{__currentTimestamp}'
          uid_foreign:
            value: '{SaveToDatabase.insertedUids.0}'
          showinpreview:
            value: '1'
      -
        table: sys_file_reference
        mode: insert
        elements:
          files:
            mapOnDatabaseColumn: uid_local
            skipIfValueIsEmpty: true
        databaseColumnMappings:
          tablenames:
            value: tx_news_domain_model_news
          fieldname:
            value: fal_related_files
          tstamp:
            value: '{__currentTimestamp}'
          crdate:
            value: '{__currentTimestamp}'
          uid_foreign:
            value: '{SaveToDatabase.insertedUids.0}'
      -
        table: sys_file_reference
        mode: update
        whereClause:
          uid_foreign: '{SaveToDatabase.insertedUids.0}'
          uid_local: 0
        databaseColumnMappings:
          pid:
            value: 0
          uid_foreign:
            value: 0
      -
        table: sys_category_record_mm
        mode: insert
        elements:
          eventcategories:
            mapOnDatabaseColumn: uid_local
            skipIfValueIsEmpty: true
        databaseColumnMappings:
          tablenames:
            value: tx_news_domain_model_news
          fieldname:
            value: categories
          uid_foreign:
            value: '{SaveToDatabase.insertedUids.0}'
      -
        table: sys_category_record_mm
        mode: update
        whereClause:
          uid_foreign: '{SaveToDatabase.insertedUids.0}'
          uid_local: 0
        databaseColumnMappings:
          uid_foreign:
            value: 0
    identifier: SaveToDatabase

Since TYPO3 13 (with DBAL 4) I get the following error when submitting the form:

[CRITICAL] request=“54d29266d8f81” component=“TYPO3.CMS.Core.Error.ProductionExceptionHandler”: Core: Exception handler (WEB: FE): Doctrine\DBAL\Exception\DriverException, code #0, file /…/vendor/doctrine/dbal/src/Driver/API/MySQL/ExceptionConverter.php, line 102: An exception occurred in the driver: No identity value was generated by the last statement. – {mode“WEB”,application_mode“FE”,exception_class“Doctrine\\DBAL\\Exception\\DriverException”,“exception_code”:0,“file”:”/…/othaw/vendor/doctrine/dbal/src/Driver/API/MySQL/ExceptionConverter.php”,“line”:102,“message”:“An exception occurred in the driver: No identity value was generated by the last statement.”,request_url“https://domain.tld/path/to/page/?tx_form_formframework%5Baction%5D=perform&tx_form_formframework%5Bcontroller%5D=FormFrontend&cHash=d87b6486e8dedd2560adea41ae417298”,exception

When I remove the last two SQL queries for sys_category_record_mm the error is gone. But then I don’t habe a relation to the selected category.

I think this is because in DBAL changelog there is:
Connection::lastInsertId() throws an exception when there’s no identity value.
(https://github.com/doctrine/dbal/blob/4.0.0/UPGRADE.md)

And sys_category__record_mm has no uid.

I think this error should be catched here:

    /**
     * Save or insert the values from
     * $databaseData into the table $table
     */
    protected function saveToDatabase(array $databaseData, string $table, int $iterationCount)
    {
        if (!empty($databaseData)) {
            if ($this->parseOption('mode') === 'update') {
                $whereClause = $this->parseOption('whereClause');
                foreach ($whereClause as $columnName => $columnValue) {
                    $whereClause[$columnName] = $this->parseOption('whereClause.' . $columnName);
                }
                $this->databaseConnection->update(
                    $table,
                    $databaseData,
                    $whereClause
                );
            } else {
                $this->databaseConnection->insert($table, $databaseData);
                $insertedUid = (int)$this->databaseConnection->lastInsertId();
                $this->finisherContext->getFinisherVariableProvider()->add(
                    $this->shortFinisherIdentifier,
                    'insertedUids.' . $iterationCount,
                    $insertedUid
                );
            }
        }
    }

Reviews 2

Files 1

History 1016

  1. Garvin Hicking commented and changed status New Needs Feedback <span data-processor="relativetime" data-value="2026-03-02T20:35:03+00:00" > 03/02/26, 20:35 </span>

    That sounds quite good! Could you enter a patch placing a try/catch in the code you located? Then we can run it through the functional tests…

  2. Bernhard Eckl commented and changed attached SaveToDatabaseFinisher_lastInsertId.diff <span data-processor="relativetime" data-value="2026-03-03T08:01:16+00:00" > 03/03/26, 08:01 </span>

    Here is my patch file. It works on my installation.

  3. Gerrit Code Review pushed patch set 1 for main to change 93040 status Needs Feedback Under Review <span data-processor="relativetime" data-value="2026-03-03T08:10:02+00:00" > 03/03/26, 08:10 </span>

  4. Garvin Hicking commented <span data-processor="relativetime" data-value="2026-03-03T08:13:05+00:00" > 03/03/26, 08:13 </span>

    Hehe, I was on it for a while and also added a test for it. Pushed a patch, would be happy if you could try it (it’s basically the same as yours with the exception that I import the namespace and do not use $e ;)

    Happy for a vote/review on that patch to move things forward.

  5. Bernhard Eckl commented <span data-processor="relativetime" data-value="2026-03-04T08:27:56+00:00" > 03/04/26, 08:27 </span>

    Your patch also works. But I would not set lastinserteduid to 0 because it is not 0, it is just not to set and the last id is still the one from the previous insert SQL.

  6. Garvin Hicking commented and changed status Under Review Resolved done 0 % 100 % <span data-processor="relativetime" data-value="2026-03-04T14:00:09+00:00" > 03/04/26, 14:00 </span>

    Applied in changeset commit:13d126218320317f1db79e5c88e0d8f5694b195e.

  7. Bernhard Eckl commented <span data-processor="relativetime" data-value="2026-03-05T10:23:19+00:00" > 03/05/26, 10:23 </span>

    I’m sorry, but it seems the patch doesn’t fix the issue. It worked some time, maybe I didn’t have the insert in the form for testing. Sorry.

  8. Garvin Hicking commented <span data-processor="relativetime" data-value="2026-03-05T10:41:10+00:00" > 03/05/26, 10:41 </span>

    I cannot reopen this easily. You can comment here in the issue or we create a followup.

    Please describe how you applied the match, and whether you’re on v13 or v14.

    Did you manually apply the patch and maybe forgot the “use Doctrine\DBAL\Exception” statement?

  9. Bernhard Eckl commented <span data-processor="relativetime" data-value="2026-03-05T10:59:04+00:00" > 03/05/26, 10:59 </span>

    You are so right! At last I was missing the use statement. Everything is good now.