[BUGFIX] Backend support for native DateTime
authorNico de Haen <mail@ndh-websolutions.de>
Mon, 15 Apr 2013 07:15:01 +0000 (09:15 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Tue, 23 Apr 2013 19:50:09 +0000 (21:50 +0200)
The getPlainValue method returns a Unix timestamp for DateTime objects.
Thus it is not possible to store native DateTime objects in
database columns of type Date or DateTime.
This patch passes the columnMap as (optional) argument to
the getPlainValue method and uses the new getDateTimeStorageFormat
method to detect the correct format

Change-Id: I6f8cdd9d131f32e46bf9a99b9c4996d48188312e
Fixes: #47223
Releases:6.0,6.1
Reviewed-on: https://review.typo3.org/19979
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php
typo3/sysext/extbase/Tests/Unit/Persistence/Generic/BackendTest.php

index 3eeeb71..78c29dd 100644 (file)
@@ -410,7 +410,7 @@ class Backend implements \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
                                }
                                $queue[] = $propertyValue;
                        } elseif ($object->_isNew() || $object->_isDirty($propertyName)) {
-                               $row[$columnMap->getColumnName()] = $this->getPlainValue($propertyValue);
+                               $row[$columnMap->getColumnName()] = $this->getPlainValue($propertyValue, $columnMap);
                        }
                }
                if (count($row) > 0) {
@@ -1017,11 +1017,21 @@ class Backend implements \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
         * Returns a plain value, i.e. objects are flattened out if possible.
         *
         * @param mixed $input
+        * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap $columnMap
         * @return mixed
         */
-       protected function getPlainValue($input) {
+       protected function getPlainValue($input, $columnMap = NULL) {
                if ($input instanceof \DateTime) {
-                       return $input->format('U');
+                       if (!is_null($columnMap) && !is_null($columnMap->getDateTimeStorageFormat())) {
+                               if ($columnMap->getDateTimeStorageFormat() == 'datetime') {
+                                       return $input->format('Y-m-d H:i:s');
+                               }
+                               if ($columnMap->getDateTimeStorageFormat() == 'date') {
+                                       return $input->format('Y-m-d');
+                               }
+                       } else {
+                               return $input->format('U');
+                       }
                } elseif ($input instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
                        return $input->getUid();
                } elseif (is_bool($input)) {
@@ -1032,4 +1042,4 @@ class Backend implements \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
        }
 }
 
-?>
\ No newline at end of file
+?>
index b902a7b..0afeddb 100644 (file)
@@ -82,5 +82,19 @@ class BackendTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $fixture->_call('insertRelationInRelationtable', $domainObject, $domainObject, '');
        }
 
+       /**
+        * @test
+        */
+       public function getPlainValueReturnsCorrectDateTimeFormat() {
+               $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend', array('dummy'), array(), '', FALSE);
+               $columnMap = new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('column_name', 'propertyName');
+               $columnMap->setDateTimeStorageFormat('datetime');
+               $datetimeAsString = '2013-04-15 09:30:00';
+               $input = new \DateTime($datetimeAsString);
+               $this->assertEquals('2013-04-15 09:30:00', $fixture->_call('getPlainValue', $input, $columnMap));
+               $columnMap->setDateTimeStorageFormat('date');
+               $this->assertEquals('2013-04-15', $fixture->_call('getPlainValue', $input, $columnMap));
+       }
+
 }
 ?>
\ No newline at end of file