如何在Symfony中使用Docgine 2的Postgres date_trunc(text, timestamp)
函数?
答案 0 :(得分:2)
<强> DateTrunc.php 强>
<?php
namespace YOUR_BUNDLE_HERE\DoctrineExtensions;
use Doctrine\ORM\Query\AST\Functions\FunctionNode,
Doctrine\ORM\Query\Lexer;
/**
* DateTrunc ::= "date_trunc" "(" ArithmeticPrimary "," ArithmeticPrimary ")"
*/
class DateTrunc extends FunctionNode
{
// (1)
public $firstDateExpression = null;
public $secondDateExpression = null;
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER); // (2)
$parser->match(Lexer::T_OPEN_PARENTHESIS); // (3)
$this->firstDateExpression = $parser->ArithmeticPrimary(); // (4)
$parser->match(Lexer::T_COMMA); // (5)
$this->secondDateExpression = $parser->ArithmeticPrimary(); // (6)
$parser->match(Lexer::T_CLOSE_PARENTHESIS); // (3)
}
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker){
return 'date_trunc(' .
$this->firstDateExpression->dispatch($sqlWalker) . ', ' .
$this->secondDateExpression->dispatch($sqlWalker) .
')'; // (7)
}
}
<强> config.yml 强>
orm:
dql:
datetime_functions:
date_trunc: YOUR_BUNDLE_HERE\DoctrineExtensions\DateTrunc
5。您现在可以将date_trunc(text, timestamp)
与Doctrine一起使用!
注意:您可以轻松地为每个额外的Postgres / MySQL功能调整此代码。结帐DoctrineExtensions