Daw*_*hia 8 datetime doctrine date dql symfony
我有一个名为Auction的实体,其中包含两个datetime类型的字段:date1和date2.我想选择所有拍卖行,其中date1和date2之间的差异小于30分钟.如何使用Doctrine查询语言执行此操作?
DATE_DIFF(date1,date2)作为文档说明"计算date1-date2之间的天数差异".我需要差异的地方.
编辑:所以这里是自定义函数TIME_DIFF的完整实现:
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
/**
* Custom DQL function returning the difference between two DateTime values
*
* usage TIME_DIFF(dateTime1, dateTime2)
*/
class TimeDiff extends FunctionNode
{
/**
* @var string
*/
public $dateTime1;
/**
* @var string
*/
public $dateTime2;
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->dateTime1 = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_COMMA);
$this->dateTime2 = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return 'TIME_TO_SEC(TIMEDIFF(' .
$this->dateTime1->dispatch($sqlWalker) . ', ' .
$this->dateTime2->dispatch($sqlWalker) .
'))';
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的app/config/config.yml中添加:
doctrine:
(...)
orm:
(...)
dql:
numeric_functions:
time_diff: MyProject\DQL\TimeDiff
Run Code Online (Sandbox Code Playgroud)
您可以使用 MySQL 的 TIMEDIFF (使用 TIME_TO_SEC 并除以 60):
TIME_TO_SEC(TIMEDIFF(date1, date2)) / 60;
Run Code Online (Sandbox Code Playgroud)
我相信你可以在 Doctrine2 中实现它,就像DATE_DIFF 的示例一样。
归档时间: |
|
查看次数: |
7635 次 |
最近记录: |