Skip to content

Commit

Permalink
Fix ISO datetime from export with CakePHP cannot be imported in MySQL5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Apr 26, 2017
1 parent b1830b8 commit 5bd3485
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
28 changes: 24 additions & 4 deletions src/Database/Type/DateTimeType.php
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Type/DateType.php
Expand Up @@ -34,7 +34,7 @@ class DateType extends DateTimeType
/**
* Date format for DateTime object
*
* @var string
* @var string|array
*/
protected $_format = 'Y-m-d';

Expand Down
2 changes: 1 addition & 1 deletion src/Database/Type/TimeType.php
Expand Up @@ -25,7 +25,7 @@ class TimeType extends DateTimeType
/**
* Time format for DateTime object
*
* @var string
* @var string|array
*/
protected $_format = 'H:i:s';

Expand Down

0 comments on commit 5bd3485

Please sign in to comment.