Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit a41e774

Browse files
committed
Merge pull request doctrine#1314 from FabioBatSilva/date-add-second
DATE_ADD - Support for seconds
2 parents 9422362 + 57bcd7c commit a41e774

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

lib/Doctrine/ORM/Query/AST/Functions/DateAddFunction.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class DateAddFunction extends FunctionNode
4545
public function getSql(SqlWalker $sqlWalker)
4646
{
4747
switch (strtolower($this->unit->value)) {
48+
case 'second':
49+
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddSecondsExpression(
50+
$this->firstDateExpression->dispatch($sqlWalker),
51+
$this->intervalExpression->dispatch($sqlWalker)
52+
);
53+
4854
case 'hour':
4955
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddHourExpression(
5056
$this->firstDateExpression->dispatch($sqlWalker),
@@ -64,7 +70,7 @@ public function getSql(SqlWalker $sqlWalker)
6470

6571
default:
6672
throw QueryException::semanticalError(
67-
'DATE_ADD() only supports units of type hour, day and month.'
73+
'DATE_ADD() only supports units of type second, hour, day and month.'
6874
);
6975
}
7076
}

tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,23 @@ public function testDateAdd()
300300
$this->assertTrue(strtotime($arg[0]['add']) > 0);
301301
}
302302

303+
public function testDateAddSecond()
304+
{
305+
$dql = "SELECT CURRENT_TIMESTAMP() now, DATE_ADD(CURRENT_TIMESTAMP(), 10, 'second') AS add FROM Doctrine\Tests\Models\Company\CompanyManager m";
306+
$query = $this->_em->createQuery($dql)->setMaxResults(1);
307+
$result = $query->getArrayResult();
308+
309+
$this->assertCount(1, $result);
310+
$this->assertArrayHasKey('now', $result[0]);
311+
$this->assertArrayHasKey('add', $result[0]);
312+
313+
$now = strtotime($result[0]['now']);
314+
$add = strtotime($result[0]['add']);
315+
$diff = $add - $now;
316+
317+
$this->assertSQLEquals(10, $diff);
318+
}
319+
303320
/**
304321
* @group DDC-1014
305322
*/

0 commit comments

Comments
 (0)