From 5bd3485a7d8e6e9ace109b47ac1e0805018f5d79 Mon Sep 17 00:00:00 2001 From: mscherer Date: Wed, 26 Apr 2017 18:27:22 +0200 Subject: [PATCH] Fix ISO datetime from export with CakePHP cannot be imported in MySQL5.7 --- src/Database/Type/DateTimeType.php | 28 ++++++++++++++++++++++++---- src/Database/Type/DateType.php | 2 +- src/Database/Type/TimeType.php | 2 +- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Database/Type/DateTimeType.php b/src/Database/Type/DateTimeType.php index 10a0675c136..5f437dadab2 100644 --- a/src/Database/Type/DateTimeType.php +++ b/src/Database/Type/DateTimeType.php @@ -53,9 +53,12 @@ class DateTimeType extends Type implements TypeInterface /** * String format to use for DateTime parsing * - * @var string + * @var string|array */ - protected $_format = 'Y-m-d H:i:s'; + protected $_format = [ + 'Y-m-d H:i:s', + 'Y-m-d\TH:i:s.uO', + ]; /** * Whether dates should be parsed using a locale aware parser @@ -114,7 +117,8 @@ public function toDatabase($value, Driver $driver) $value = new $class('@' . $value); } - return $value->format($this->_format); + $format = (array)$this->_format; + return $value->format(array_shift($format)); } /** @@ -165,7 +169,7 @@ public function marshal($value) $date = new $class($value); $compare = true; } - if ($compare && $date && $date->format($this->_format) !== $value) { + if ($compare && $date && !$this->_compare($date, $value)) { return $value; } if ($date) { @@ -205,6 +209,22 @@ public function marshal($value) return new $class($format, $tz); } + /** + * @param \Cake\I18n\Time|\DateTime $date + * @param mixed $value Request data + * @return bool + */ + protected function _compare($date, $value) + { + foreach ((array)$this->_format as $format) { + if ($date->format($format) === $value) { + return true; + } + } + + return false; + } + /** * Sets whether or not to parse dates passed to the marshal() function * by using a locale aware parser. diff --git a/src/Database/Type/DateType.php b/src/Database/Type/DateType.php index d90ef31a16d..be4369d3d79 100644 --- a/src/Database/Type/DateType.php +++ b/src/Database/Type/DateType.php @@ -34,7 +34,7 @@ class DateType extends DateTimeType /** * Date format for DateTime object * - * @var string + * @var string|array */ protected $_format = 'Y-m-d'; diff --git a/src/Database/Type/TimeType.php b/src/Database/Type/TimeType.php index 0b7c0aa2a18..4293ab2518e 100644 --- a/src/Database/Type/TimeType.php +++ b/src/Database/Type/TimeType.php @@ -25,7 +25,7 @@ class TimeType extends DateTimeType /** * Time format for DateTime object * - * @var string + * @var string|array */ protected $_format = 'H:i:s';