Commit feedcfae authored by Benjamin Eberlei's avatar Benjamin Eberlei

[DBAL-415] More lenient parsing of DateTime by using date_create() as fallback.

parent 269d3e1f
# Upgrade to 2.5 # Upgrade to 2.5
## datetime Type uses date_create() as fallback
Before 2.5 the DateTime type always required a specific format, defined in
`$platform->getDateTimeFormatString()`, which could cause quite some troubles
on platforms that had various microtime precision formats. Starting with 2.5
whenever the parsing of a date fails with the predefined platform format,
the `date_create()` function will be used to parse the date.
This could cause some troubles when your date format is weird and not parsed
correctly by `date_create`, however since databases are rather strict on dates
there should be no problem.
## Support for pdo_ibm driver removed ## Support for pdo_ibm driver removed
The ``pdo_ibm`` driver is buggy and does not work well with Doctrine. Therefore it will no The ``pdo_ibm`` driver is buggy and does not work well with Doctrine. Therefore it will no
......
...@@ -63,6 +63,11 @@ class DateTimeType extends Type ...@@ -63,6 +63,11 @@ class DateTimeType extends Type
} }
$val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value); $val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value);
if ( ! $val) {
$val = date_create($value);
}
if ( ! $val) { if ( ! $val) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString()); throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
} }
......
...@@ -53,4 +53,13 @@ class DateTimeTest extends \Doctrine\Tests\DbalTestCase ...@@ -53,4 +53,13 @@ class DateTimeTest extends \Doctrine\Tests\DbalTestCase
$date = new \DateTime("now"); $date = new \DateTime("now");
$this->assertSame($date, $this->_type->convertToPHPValue($date, $this->_platform)); $this->assertSame($date, $this->_type->convertToPHPValue($date, $this->_platform));
} }
public function testConvertsNonMatchingFormatToPhpValueWithParser()
{
$date = '1985/09/01 10:10:10.12345';
$actual = $this->_type->convertToPHPValue($date, $this->_platform);
$this->assertEquals('1985-09-01 10:10:10', $actual->format('Y-m-d H:i:s'));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment