1

how to use datetime functions in doctrine query builder,the below sql query need convert doctrine query(DQL).

SELECT SEC_TO_TIME((SUM(TIMESTAMPDIFF(MINUTE, StartTime, EndTime)) + COUNT(*)) * 60) as hours FROM table_name

The below one i have tried

$qb = $emClient->createQueryBuilder()->select('SEC_TO_TIME((SUM(TIMESTAMPDIFF(MINUTE, en.startTime, swenendTime)) + COUNT(*)) * 60) as hours')
            ->from('AppBundle:MyEntity', 'en')
$result = $qb->getQuery()->getArrayResult();

But i got error undefined keyword at column 0,7

Thanks in advance

Robert
  • 3,373
  • 1
  • 18
  • 34

2 Answers2

3

Doctrine supports following built-in functions http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#functions-operators-aggregates

So either you implement your own functions or use plain SQL query $emClient->getConnection()->executeQuery()

Denis Alimov
  • 2,861
  • 1
  • 18
  • 38
1

In order to use these function either you need to implement them on your own if they don't exist as @Denis Alimov suggested or you can use already implemented function with Doctrine Extensions Bundle. The last option is get all the data needed and perform some operations on PHP side. You can also execute PURE sql and map it to doctrine with ResultSetMappingBuilder

Robert
  • 19,800
  • 5
  • 55
  • 85