I'm trying to implement a custom function to get the time difference on two fields but I keep getting errors... I've read similar errors but the answers haven't really helped.
I've been doing this:
<?php
namespace Bundle\DQL;
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 TimeDiffFunction 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) .
'))';
}
}
config.yml
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
dql:
numeric_functions:
time_diff: Bundle:DQL:TimeDiffFunction
controller:
$em = $this->getDoctrine()->getManager();
$detail1 = $em->getRepository('Bundle:Detail')->find(96);
//$detail2 = $em->getRepository('Bundle:Detail')->find(138);
$query = $em->createQuery("SELECT time_diff(eng.updatedDate, eng.updatedDate) FROM Bundle\Entity\Detail eng WHERE eng.id = :id1");
$query->setParameter('id1', $detail1->getId());
$entities = $query->getResult();
die(var_dump($entities));
doing that I get:
FatalErrorException: Error: Class 'Bundle:DQL:TimeDiffFunction' not found in C:\xampp\htdocs\EngMgmt\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php line 3070
but doing this on the controller instead
$query = $em->createQuery("SELECT Bundle\DQL\TimeDiff(eng.updatedDate, eng.updatedDate) FROM Bundle\Entity\Detail eng WHERE eng.id = :id1");
i get:
[Syntax Error] line 0, col 7: Error: Expected known function, got 'Bundle\DQL\TimeDiff'
and If i do this on the controller:
$query = $em->createQuery("SELECT Bundle\DQL\time_diff(eng.updatedDate, eng.updatedDate) Bundle\Entity\Detail eng WHERE eng.id = :id1");
i get:
[Syntax Error] line 0, col 47: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM, got '('
so, the question is, how can I make it work? I REALLY need it.