From 57bcd7cd3da59647eb46f4e1c308e6d609ac1394 Mon Sep 17 00:00:00 2001 From: FabioBatSilva Date: Wed, 25 Feb 2015 14:32:51 -0500 Subject: [PATCH] DATE_ADD - Support for seconds --- .../ORM/Query/AST/Functions/DateAddFunction.php | 8 +++++++- .../ORM/Functional/QueryDqlFunctionTest.php | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Query/AST/Functions/DateAddFunction.php b/lib/Doctrine/ORM/Query/AST/Functions/DateAddFunction.php index d78cd976a..312a52c67 100644 --- a/lib/Doctrine/ORM/Query/AST/Functions/DateAddFunction.php +++ b/lib/Doctrine/ORM/Query/AST/Functions/DateAddFunction.php @@ -45,6 +45,12 @@ class DateAddFunction extends FunctionNode public function getSql(SqlWalker $sqlWalker) { switch (strtolower($this->unit->value)) { + case 'second': + return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddSecondsExpression( + $this->firstDateExpression->dispatch($sqlWalker), + $this->intervalExpression->dispatch($sqlWalker) + ); + case 'hour': return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddHourExpression( $this->firstDateExpression->dispatch($sqlWalker), @@ -64,7 +70,7 @@ class DateAddFunction extends FunctionNode default: throw QueryException::semanticalError( - 'DATE_ADD() only supports units of type hour, day and month.' + 'DATE_ADD() only supports units of type second, hour, day and month.' ); } } diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php index a28f0ccaa..6478fc404 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php @@ -300,6 +300,23 @@ class QueryDqlFunctionTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertTrue(strtotime($arg[0]['add']) > 0); } + public function testDateAddSecond() + { + $dql = "SELECT CURRENT_TIMESTAMP() now, DATE_ADD(CURRENT_TIMESTAMP(), 10, 'second') AS add FROM Doctrine\Tests\Models\Company\CompanyManager m"; + $query = $this->_em->createQuery($dql)->setMaxResults(1); + $result = $query->getArrayResult(); + + $this->assertCount(1, $result); + $this->assertArrayHasKey('now', $result[0]); + $this->assertArrayHasKey('add', $result[0]); + + $now = strtotime($result[0]['now']); + $add = strtotime($result[0]['add']); + $diff = $add - $now; + + $this->assertSQLEquals(10, $diff); + } + /** * @group DDC-1014 */