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