[TASK] Ext:form - Add update option to saveToDatabase finisher 10/50810/4
authorRalf Zimmermann <ralf.zimmermann@tritum.de>
Wed, 30 Nov 2016 02:08:30 +0000 (03:08 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Wed, 25 Jan 2017 12:45:00 +0000 (13:45 +0100)
This patch adds an option to the saveToDatabase finisher which makes it
possible to update an existing database record.
The forge issue provides an example extension for testing.

Resolves: #78839
Releases: master
Change-Id: I07d874a72b81a5e43c864f8a468a1310cac57995
Reviewed-on: https://review.typo3.org/50810
Reviewed-by: Bjoern Jacob <bjoern.jacob@tritum.de>
Tested-by: Bjoern Jacob <bjoern.jacob@tritum.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Daniel Lorenz <daniel.lorenz@extco.de>
Tested-by: Daniel Lorenz <daniel.lorenz@extco.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/form/Classes/Domain/Finishers/SaveToDatabaseFinisher.php
typo3/sysext/form/Configuration/Yaml/BaseSetup.yaml

index bb7eb27..cb15100 100644 (file)
@@ -35,6 +35,8 @@ class SaveToDatabaseFinisher extends AbstractFinisher
      */
     protected $defaultOptions = [
         'table' => null,
+        'mode' => 'insert',
+        'whereClause' => [],
         'elements' => [],
     ];
 
@@ -47,6 +49,16 @@ class SaveToDatabaseFinisher extends AbstractFinisher
      */
     protected function executeInternal()
     {
+        if (
+            $this->options['mode'] === 'update'
+            && empty($this->options['whereClause'])
+        ) {
+            throw new FinisherException(
+                'An empty option "whereClause" is not allowed in update mode.',
+                1480469086
+            );
+        }
+
         $table = $this->parseOption('table');
         $elementsConfiguration = $this->parseOption('elements');
 
@@ -60,7 +72,10 @@ class SaveToDatabaseFinisher extends AbstractFinisher
         $databaseColumns = $schemaManager->listTableColumns($table);
         foreach ($elementsConfiguration as $elementIdentifier => $elementConfiguration) {
             if (!array_key_exists($elementConfiguration['mapOnDatabaseColumn'], $databaseColumns)) {
-                throw new FinisherException('The column "' . $elementConfiguration['mapOnDatabaseColumn'] . '" does not exist in table "' . $table . '".', 1476362572);
+                throw new FinisherException(
+                    'The column "' . $elementConfiguration['mapOnDatabaseColumn'] . '" does not exist in table "' . $table . '".',
+                    1476362572
+                );
             }
         }
 
@@ -68,6 +83,15 @@ class SaveToDatabaseFinisher extends AbstractFinisher
 
         $insertData = [];
         foreach ($this->finisherContext->getFormValues() as $elementIdentifier => $elementValue) {
+            if (
+                $elementValue === null
+                && isset($elementsConfiguration[$elementIdentifier])
+                && isset($elementsConfiguration[$elementIdentifier]['skipIfValueIsNull'])
+                && $elementsConfiguration[$elementIdentifier]['skipIfValueIsNull'] === true
+            ) {
+                continue;
+            }
+
             $element = $formRuntime->getFormDefinition()->getElementByIdentifier($elementIdentifier);
             if (
                 !$element instanceof FormElementInterface
@@ -94,7 +118,15 @@ class SaveToDatabaseFinisher extends AbstractFinisher
         }
 
         if (!empty($insertData)) {
-            $databaseConnection->insert($table, $insertData);
+            if ($this->options['mode'] === 'update') {
+                $databaseConnection->update(
+                    $table,
+                    $insertData,
+                    $this->options['whereClause']
+                );
+            } else {
+                $databaseConnection->insert($table, $insertData);
+            }
         }
     }
 }
index 31733d1..9a06c9b 100644 (file)
@@ -202,10 +202,13 @@ TYPO3:
               implementationClassName: 'TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher'
               #options:
                 #table: ''
+                #mode: 'insert'
+                #whereClause: []
                 #elements:
                 #  <elementIdentifier>:
                 #    mapOnDatabaseColumn: sender_name
-                #    saveFileIdentifierInsteadOfUid: 'false'
+                #    saveFileIdentifierInsteadOfUid: false
+                #    skipIfValueIsNull: false
 
           ### VALIDATORS ###
           validatorsDefinition: