I would like to format a date with Doctrine, I often save a date as datetime in the database however I want to perform a grouped search per day without considering minutes and seconds. Does anyone know a cool way to do this?
I would like to format a date with Doctrine, I often save a date as datetime in the database however I want to perform a grouped search per day without considering minutes and seconds. Does anyone know a cool way to do this?
The doctrine does not yet have the full range of SQL functions, they are working on it for future versions as the cookbook . Until then you have to create the functions that are missing, in my case create the function DATE
namespace ByteinCoffee\ExtraBundle\Doctrine\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
* @author Fábio Lemos Elizandro <[email protected]>
*
* DateFunction ::= "DATE" "(" ArithmeticPrimary ")"
*/
class Date extends FunctionNode
{
public $dateExpression = null;
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->dateExpression = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(SqlWalker $sqlWalker)
{
return \sprintf('DATE(%s)', $this->dateExpression->dispatch($sqlWalker));
}
}
to register the function
doctrine:
#......
orm:
dql:
datetime_functions:
date: ByteinCoffee\ExtraBundle\Doctrine\DQL\Date
If someone knows a bundle that does this leave it in response as well. Thanks