From 827219a1c35b4aca6dbab5855a36e9277b2ec8f4 Mon Sep 17 00:00:00 2001 From: Andreas Wolf <andreas.wolf@typo3.org> Date: Sun, 6 Mar 2016 13:46:35 +0100 Subject: [PATCH] [BUGFIX] Extbase cannot persist to datetime fields This patch adds missing column configuration to DataMapper call. Resolves: #74376 Releases: master, 7.6 Change-Id: I505a5e9f05338fde48fad9f49391095e47523409 Reviewed-on: https://review.typo3.org/47136 Reviewed-by: Nicole Cordes <typo3@cordes.co> Tested-by: Nicole Cordes <typo3@cordes.co> Reviewed-by: Manuel Selbach <manuel_selbach@yahoo.de> Reviewed-by: Benni Mack <benni@typo3.org> Tested-by: Benni Mack <benni@typo3.org> --- .../Classes/Persistence/Generic/Backend.php | 2 +- .../Persistence/Generic/Mapper/DataMapper.php | 7 ++- .../tx_blogexample_domain_model_comment.php | 1 + .../Extensions/blog_example/ext_tables.sql | 2 +- .../Generic/Mapper/DataMapperTest.php | 63 +++++++++++++++++++ 5 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 typo3/sysext/extbase/Tests/Functional/Persistence/Generic/Mapper/DataMapperTest.php diff --git a/typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php b/typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php index db0f3107e640..46654d8050c2 100644 --- a/typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php +++ b/typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php @@ -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); diff --git a/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapper.php b/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapper.php index b99910eae999..e4e4ac13fd78 100755 --- a/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapper.php +++ b/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapper.php @@ -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); diff --git a/typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Configuration/TCA/tx_blogexample_domain_model_comment.php b/typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Configuration/TCA/tx_blogexample_domain_model_comment.php index d668804bf923..6ea7a3b03902 100644 --- a/typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Configuration/TCA/tx_blogexample_domain_model_comment.php +++ b/typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Configuration/TCA/tx_blogexample_domain_model_comment.php @@ -29,6 +29,7 @@ return array( 'label' => 'LLL:EXT:blog_example/Resources/Private/Language/locallang_db.xml:tx_blogexample_domain_model_comment.date', 'config' => array( 'type' => 'input', + 'dbType' => 'datetime', 'size' => 12, 'eval' => 'datetime, required', 'default' => time() diff --git a/typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/ext_tables.sql b/typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/ext_tables.sql index 67ef7cf09ae7..0323d3608124 100644 --- a/typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/ext_tables.sql +++ b/typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/ext_tables.sql @@ -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 index 000000000000..879cbbdcb987 --- /dev/null +++ b/typo3/sysext/extbase/Tests/Functional/Persistence/Generic/Mapper/DataMapperTest.php @@ -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()); + } +} -- GitLab