[BUGFIX] Allow subclasses of \DateTime as entity properties 90/42590/2
authorStephan Schuler <stephan.schuler@netlogix.de>
Wed, 13 May 2015 13:05:35 +0000 (15:05 +0200)
committerAndreas Wolf <andreas.wolf@typo3.org>
Thu, 13 Aug 2015 19:34:49 +0000 (21:34 +0200)
When a class "\MyCustomDateTime extends \DateTime", the data mapper
creates "\DateTime" objects instead when mapping database results.
This patch uses the class annotation to resolve the class to create.

Resolves: #66939
Releases: master, 6.2
Change-Id: I45062d4d1a44333016258289ea9e2026d9923f70
Reviewed-on: http://review.typo3.org/42590
Reviewed-by: Andreas Wolf <andreas.wolf@typo3.org>
Tested-by: Andreas Wolf <andreas.wolf@typo3.org>
typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapper.php [changed mode: 0644->0755]
typo3/sysext/extbase/Tests/Unit/Persistence/Fixture/Model/CustomDateTime.php [new file with mode: 0644]
typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Mapper/DataMapperTest.php

old mode 100644 (file)
new mode 100755 (executable)
index 28f31f9..c168fbf
@@ -219,7 +219,7 @@ class DataMapper implements \TYPO3\CMS\Core\SingletonInterface {
                                                break;
                                        default:
                                                if ($propertyData['type'] === 'DateTime' || in_array('DateTime', class_parents($propertyData['type']))) {
-                                                       $propertyValue = $this->mapDateTime($row[$columnName], $columnMap->getDateTimeStorageFormat());
+                                                       $propertyValue = $this->mapDateTime($row[$columnName], $columnMap->getDateTimeStorageFormat(), $propertyData['type']);
                                                } elseif (TypeHandlingUtility::isCoreType($propertyData['type'])) {
                                                        $propertyValue = $this->mapCoreType($propertyData['type'], $row[$columnName]);
                                                } else {
@@ -255,20 +255,21 @@ class DataMapper implements \TYPO3\CMS\Core\SingletonInterface {
         *
         * @param integer|string $value Unix timestamp or date/datetime value
         * @param NULL|string $storageFormat Storage format for native date/datetime fields
+        * @param NULL|string $targetType The object class name to be created
         * @return \DateTime
         */
-       protected function mapDateTime($value, $storageFormat = NULL) {
+       protected function mapDateTime($value, $storageFormat = NULL, $targetType = 'DateTime') {
                if (empty($value) || $value === '0000-00-00' || $value === '0000-00-00 00:00:00') {
                        // 0 -> NULL !!!
                        return NULL;
                } elseif ($storageFormat === 'date' || $storageFormat === 'datetime') {
                        // native date/datetime values are stored in UTC
                        $utcTimeZone = new \DateTimeZone('UTC');
-                       $utcDateTime = new \DateTime($value, $utcTimeZone);
+                       $utcDateTime = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($targetType, $value, $utcTimeZone);
                        $currentTimeZone = new \DateTimeZone(date_default_timezone_get());
                        return $utcDateTime->setTimezone($currentTimeZone);
                } else {
-                       return new \DateTime(date('c', $value));
+                       return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($targetType, date('c', $value));
                }
        }
 
diff --git a/typo3/sysext/extbase/Tests/Unit/Persistence/Fixture/Model/CustomDateTime.php b/typo3/sysext/extbase/Tests/Unit/Persistence/Fixture/Model/CustomDateTime.php
new file mode 100644 (file)
index 0000000..3a51ec1
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Fixture\Model;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+class CustomDateTime extends \DateTime {
+
+}
index f1fb538..12d030b 100644 (file)
@@ -229,4 +229,22 @@ class DataMapperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        $this->assertEquals($expectedValue, $dateTime->format('c'));
                }
        }
+
+       /**
+        * @test
+        */
+       public function mapDateTimeHandlesSubclassesOfDateTime() {
+               $accessibleClassName = $this->buildAccessibleProxy('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
+               $accessibleDataMapFactory = new $accessibleClassName();
+
+               $targetType = 'TYPO3\CMS\Extbase\Tests\Unit\Persistence\Fixture\Model\CustomDateTime';
+               $date = '2013-01-01 01:02:03';
+               $storageFormat = 'datetime';
+
+               /** @var $dateTime NULL|\DateTime */
+               $dateTime = $accessibleDataMapFactory->_callRef('mapDateTime', $date, $storageFormat, $targetType);
+
+               $this->assertInstanceOf($targetType, $dateTime);
+       }
+
 }