[BUGFIX] Extbase cannot persist to datetime fields 46/47846/3
authorAndreas Wolf <andreas.wolf@typo3.org>
Sun, 6 Mar 2016 12:46:35 +0000 (13:46 +0100)
committerBenni Mack <benni@typo3.org>
Mon, 29 Aug 2016 22:37:35 +0000 (00:37 +0200)
This patch adds missing column configuration to DataMapper call.

Resolves: #74376
Releases: master, 7.6
Change-Id: I505a5e9f05338fde48fad9f49391095e47523409
Reviewed-on: https://review.typo3.org/47846
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php
typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapper.php
typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Configuration/TCA/Comment.php
typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/ext_tables.sql
typo3/sysext/extbase/Tests/Functional/Persistence/Generic/Mapper/DataMapperTest.php [new file with mode: 0644]

index 77d6d0c..f18ff6d 100644 (file)
@@ -691,7 +691,7 @@ class Backend implements \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
                     $row[$columnMap->getColumnName()] = 0;
                 }
             } elseif ($propertyValue !== null) {
-                $row[$columnMap->getColumnName()] = $this->dataMapper->getPlainValue($propertyValue);
+                $row[$columnMap->getColumnName()] = $this->dataMapper->getPlainValue($propertyValue, $columnMap);
             }
         }
         $this->addCommonFieldsToRow($object, $row);
index b99910e..e4e4ac1 100644 (file)
@@ -698,12 +698,15 @@ class DataMapper implements \TYPO3\CMS\Core\SingletonInterface
         } elseif ($input instanceof \DateTime) {
             if (!is_null($columnMap) && !is_null($columnMap->getDateTimeStorageFormat())) {
                 $storageFormat = $columnMap->getDateTimeStorageFormat();
+                $timeZoneToStore = clone $input;
+                // set to UTC to store in database
+                $timeZoneToStore->setTimezone(new \DateTimeZone('UTC'));
                 switch ($storageFormat) {
                     case 'datetime':
-                        $parameter = $input->format('Y-m-d H:i:s');
+                        $parameter = $timeZoneToStore->format('Y-m-d H:i:s');
                         break;
                     case 'date':
-                        $parameter = $input->format('Y-m-d');
+                        $parameter = $timeZoneToStore->format('Y-m-d');
                         break;
                     default:
                         throw new \InvalidArgumentException('Column map DateTime format "' . $storageFormat . '" is unknown. Allowed values are datetime or date.', 1395353470);
index 3ee9569..d5ddd2f 100644 (file)
@@ -18,6 +18,7 @@ $TCA['tx_blogexample_domain_model_comment'] = array(
             'exclude' => 1,
             'label' => 'LLL:EXT:blog_example/Resources/Private/Language/locallang_db.xml:tx_blogexample_domain_model_comment.date',
             'config' => array(
+                'dbType' => 'datetime',
                 'type' => 'input',
                 'size' => 12,
                 'eval' => 'datetime, required',
index 67ef7cf..0323d36 100644 (file)
@@ -90,7 +90,7 @@ CREATE TABLE tx_blogexample_domain_model_comment (
 
        post int(11) DEFAULT '0' NOT NULL,
 
-       date int(11) DEFAULT '0' NOT NULL,
+       date datetime,
        author varchar(255) DEFAULT '' NOT NULL,
        email varchar(255) DEFAULT '' NOT NULL,
        content text NOT NULL,
diff --git a/typo3/sysext/extbase/Tests/Functional/Persistence/Generic/Mapper/DataMapperTest.php b/typo3/sysext/extbase/Tests/Functional/Persistence/Generic/Mapper/DataMapperTest.php
new file mode 100644 (file)
index 0000000..879cbbd
--- /dev/null
@@ -0,0 +1,63 @@
+<?php
+namespace TYPO3\CMS\Extbase\Tests\Functional\Persistence\Generic\Mapper;
+
+use ExtbaseTeam\BlogExample\Domain\Model\Comment;
+use TYPO3\CMS\Core\Tests\FunctionalTestCase;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+
+class DataMapperTest extends FunctionalTestCase
+{
+
+    /**
+     * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
+     */
+    protected $persistenceManager;
+
+    /**
+     * @var array
+     */
+    protected $testExtensionsToLoad = array('typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example');
+
+    /**
+     * @var array
+     */
+    protected $coreExtensionsToLoad = array('extbase', 'fluid');
+
+    /**
+     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface The object manager
+     */
+    protected $objectManager;
+
+
+    /**
+     * Sets up this test suite.
+     */
+    protected function setUp()
+    {
+        parent::setUp();
+
+        $this->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
+        $this->persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
+    }
+
+    /**
+     * @test
+     */
+    public function datetimeObjectsCanBePersistedToDatetimeDatabaseFields()
+    {
+        $date = new \DateTime('2016-03-06T12:40:00+01:00');
+        $comment = new Comment();
+        $comment->setDate($date);
+
+        $this->persistenceManager->add($comment);
+        $this->persistenceManager->persistAll();
+        $uid = $this->persistenceManager->getIdentifierByObject($comment);
+        $this->persistenceManager->clearState();
+
+        /** @var Comment $existingComment */
+        $existingComment = $this->persistenceManager->getObjectByIdentifier($uid, Comment::class);
+
+        $this->assertEquals($date->getTimestamp(), $existingComment->getDate()->getTimestamp());
+    }
+}