Skip to content

Commit

Permalink
Merge pull request #2316 from vbartusevicius/issue-2314
Browse files Browse the repository at this point in the history
Fix date interval database value truncation (string overflow)
  • Loading branch information
Ocramius committed Jul 2, 2017
2 parents 9348849 + 9ca48dc commit 8906f73
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
10 changes: 4 additions & 6 deletions lib/Doctrine/DBAL/Types/DateIntervalType.php
Expand Up @@ -22,7 +22,7 @@ public function getName()
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$fieldDeclaration['length'] = 20;
$fieldDeclaration['length'] = 255;
$fieldDeclaration['fixed'] = true;

return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
Expand All @@ -38,9 +38,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
}

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']);
Expand All @@ -58,10 +56,10 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
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}
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php
Expand Up @@ -32,17 +32,17 @@ public function testDateIntervalConvertsToDatabaseValue()
{
$interval = new \DateInterval('P2Y1DT1H2M3S');

$expected = 'P0002-00-01T01:02:03';
$expected = 'P02Y00M01DT01H02M03S';
$actual = $this->type->convertToDatabaseValue($interval, $this->platform);

$this->assertEquals($expected, $actual);
}

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()
Expand Down

0 comments on commit 8906f73

Please sign in to comment.