doctrine2 - Postgres date_trunc(text, timestamp) with Doctrine 2 in Symfony? -
how use postgres date_trunc(text, timestamp) function doctrine 2 in symfony?
- in project, create new folder named doctrineextensions
- in new folder, create file named datetrunc.php
- paste code:
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) } } - add function doctrine configuration
config.yml
orm: dql: datetime_functions: date_trunc: your_bundle_here\doctrineextensions\datetrunc 5. can use date_trunc(text, timestamp) doctrine!
note: can adapt code every additional postgres/mysql function. checkout doctrineextensions
Comments
Post a Comment