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