Commit 8906f736 authored by Marco Pivetta's avatar Marco Pivetta Committed by GitHub

Merge pull request #2316 from vbartusevicius/issue-2314

Fix date interval database value truncation (string overflow)
parents 93488498 9ca48dc3
......@@ -22,7 +22,7 @@ class DateIntervalType extends Type
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$fieldDeclaration['length'] = 20;
$fieldDeclaration['length'] = 255;
$fieldDeclaration['fixed'] = true;
return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
......@@ -38,9 +38,7 @@ class DateIntervalType extends Type
}
if ($value instanceof \DateInterval) {
return 'P'
. str_pad($value->y, 4, '0', STR_PAD_LEFT) . '-'
. $value->format('%M-%DT%H:%I:%S');
return $value->format('P%YY%MM%DDT%HH%IM%SS');
}
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateInterval']);
......@@ -58,10 +56,10 @@ class DateIntervalType extends Type
try {
return new \DateInterval($value);
} catch (\Exception $exception) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), 'PY-m-dTH:i:s', $exception);
throw ConversionException::conversionFailedFormat($value, $this->getName(), 'P%YY%MM%DDT%HH%IM%SS', $exception);
}
}
/**
* {@inheritdoc}
*/
......
......@@ -32,7 +32,7 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
{
$interval = new \DateInterval('P2Y1DT1H2M3S');
$expected = 'P0002-00-01T01:02:03';
$expected = 'P02Y00M01DT01H02M03S';
$actual = $this->type->convertToDatabaseValue($interval, $this->platform);
$this->assertEquals($expected, $actual);
......@@ -40,9 +40,9 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
public function testDateIntervalConvertsToPHPValue()
{
$date = $this->type->convertToPHPValue('P0002-00-01T01:02:03', $this->platform);
$date = $this->type->convertToPHPValue('P02Y00M01DT01H02M03S', $this->platform);
$this->assertInstanceOf('DateInterval', $date);
$this->assertEquals('P2Y0M1DT1H2M3S', $date->format('P%yY%mM%dDT%hH%iM%sS'));
$this->assertEquals('P02Y00M01DT01H02M03S', $date->format('P%YY%MM%DDT%HH%IM%SS'));
}
public function testInvalidDateIntervalFormatConversion()
......
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