doctrine2 - Postgres date_trunc(text, timestamp) with Doctrine 2 in Symfony? -


how use postgres date_trunc(text, timestamp) function doctrine 2 in symfony?

  1. in project, create new folder named doctrineextensions
  2. in new folder, create file named datetrunc.php
  3. 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)   } } 
  1. 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

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -