Bug #85246
closedSaveToDatabase finisher should convert DateTime Objects to string
100%
Description
The SaveToDatabase finisher should convert DateTime Objects to string.
Currently this example will fail.
type: Form identifier: test1 label: test1 prototypeName: standard finishers: - identifier: SaveToDatabase options: - table: 'tt_content' mode: insert elements: 'datepicker-1': mapOnDatabaseColumn: 'tstamp' 'date-1': mapOnDatabaseColumn: 'crdate' renderables: - type: Page identifier: page-1 label: Step renderables: - type: Date identifier: date-1 label: Date defaultValue: '' - type: DatePicker identifier: datepicker-1 label: 'Date picker (jQuery)' properties: dateFormat: Y-m-d enableDatePicker: true displayTimeSelector: false
Updated by Gerrit Code Review over 6 years ago
- Status changed from New to Under Review
Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/57205
Updated by Ralf Zimmermann over 6 years ago
Testcase
type: Form identifier: test1 label: test1 prototypeName: standard finishers: - identifier: SaveToDatabase options: - table: 'tt_content' mode: insert elements: 'datepicker-1': mapOnDatabaseColumn: 'header' dateFormat: 'd.m.Y' 'date-1': mapOnDatabaseColumn: 'crdate' renderables: - type: Page identifier: page-1 label: Step renderables: - type: Date identifier: date-1 label: Date defaultValue: '' - type: DatePicker identifier: datepicker-1 label: 'Date picker (jQuery)' properties: dateFormat: Y-m-d enableDatePicker: true displayTimeSelector: false
Updated by Gerrit Code Review over 6 years ago
Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/57205
Updated by Gerrit Code Review over 6 years ago
Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/57205
Updated by Gerrit Code Review over 6 years ago
Patch set 4 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/57205
Updated by Lidia Demin over 6 years ago
I have a use case where I also configure the date picker like above to define the preview the format in frontend as the DatePickerViewHelper also uses this property. But in the database I need this field to be saved as time stamp.
My solution was to extend SaveToDatabaseFinisher and add a "convertToFormat" property. This looks like this then:
-
type: DatePicker
identifier: datepicker-1
label: 'Date picker (jQuery)'
properties:
dateFormat: Y-m-d
convertToFormat: 'U'
and in SaveToDatabaseFinisher
if ($elementValue instanceof \DateTime) {
$elementProperties = $element->getProperties();
if ($elementProperties['convertToFormat']) {
$elementValue = $elementValue->format((string)$elementProperties['convertToFormat']);
}
}
What do you think? Maybe we could make the formatting for the database more flexible. The default could be time stamp.
Updated by Ralf Zimmermann over 6 years ago
The property 'properties.dateFormat' from the form element type "DatePicker" is used to define the display format within a form step like the summary page.
https://docs.typo3.org/typo3cms/extensions/form/Config/proto/formElements/formElementTypes/DatePicker.html?highlight=dateformat#properties-dateformat
The new property 'options.<index>.elements.<datePickerFormElementIdentifier>.dateFormat' within the SavaToDatabase finisher configuration defines the "convert to" format for the database. The default is "U" (unix timestamp). This is documented here:
https://review.typo3.org/#/c/57205/4/typo3/sysext/form/Documentation/ApiReference/Index.rst
Is thatnot your use case?
The example wants to show exactly this
The DatePicker is configured to show "Y-m-d"
type: DatePicker identifier: datepicker-1 label: 'Date picker (jQuery)' properties: dateFormat: Y-m-d enableDatePicker: true displayTimeSelector: false
For the "DatePicker" element, the values for the database are in the format 'd.m.Y' and 'U' (default value because no dateFormat is defined) for the "Date" form element
finishers: - identifier: SaveToDatabase options: - table: 'tt_content' mode: insert elements: 'datepicker-1': mapOnDatabaseColumn: 'header' dateFormat: 'd.m.Y' 'date-1': mapOnDatabaseColumn: 'crdate'
The form elements property "dateFormat" is another property than "dateFormat" from within the finisher settings...
Updated by Gerrit Code Review over 6 years ago
Patch set 5 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/57205
Updated by Gerrit Code Review over 6 years ago
Patch set 1 for branch TYPO3_8-7 of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/57490
Updated by Ralf Zimmermann over 6 years ago
- Status changed from Under Review to Resolved
- % Done changed from 0 to 100
Applied in changeset 6134369f5d2a8b0a8787e1125d7aff947c07da6f.