Project

General

Profile

Actions

Bug #85246

closed

SaveToDatabase finisher should convert DateTime Objects to string

Added by Ralf Zimmermann almost 6 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Must have
Category:
Form Framework
Target version:
-
Start date:
2018-06-12
Due date:
% Done:

100%

Estimated time:
TYPO3 Version:
8
PHP Version:
Tags:
Complexity:
Is Regression:
Sprint Focus:

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

Actions #1

Updated by Gerrit Code Review almost 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

Actions #2

Updated by Ralf Zimmermann almost 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
Actions #3

Updated by Gerrit Code Review almost 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

Actions #4

Updated by Gerrit Code Review almost 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

Actions #5

Updated by Gerrit Code Review almost 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

Actions #6

Updated by Lidia Demin almost 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.

Actions #7

Updated by Ralf Zimmermann almost 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...

Actions #8

Updated by Lidia Demin almost 6 years ago

Okay, thanks ;) I've tested.

Actions #9

Updated by Gerrit Code Review almost 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

Actions #10

Updated by Gerrit Code Review almost 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

Actions #11

Updated by Ralf Zimmermann almost 6 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100
Actions #12

Updated by Benni Mack over 5 years ago

  • Status changed from Resolved to Closed
Actions

Also available in: Atom PDF