Commit 193d9b4e authored by Ralf Zimmermann's avatar Ralf Zimmermann Committed by Anja Leichsenring
Browse files

[TASK] Ext:form - Add update option to saveToDatabase finisher

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: Björn Jacob's avatarBjoern Jacob <bjoern.jacob@tritum.de>
Tested-by: Björn Jacob's avatarBjoern Jacob <bjoern.jacob@tritum.de>
Tested-by: default avatarTYPO3com <no-reply@typo3.com>
Reviewed-by: default avatarDaniel Lorenz <daniel.lorenz@extco.de>
Tested-by: default avatarDaniel Lorenz <daniel.lorenz@extco.de>
Reviewed-by: Anja Leichsenring's avatarAnja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring's avatarAnja Leichsenring <aleichsenring@ab-softlab.de>
parent fadaaccf
......@@ -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);
}
}
}
}
......@@ -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:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment