I want to retrive Event which are not yet finished.
My problem is that startTime is a DateTime and duration is an Integer.
How can I make this addition ?
This code :
$events = $this->getRepository('Event')->createQueryBuilder('e')
->where('e.startTime+e.duration > CURRENT_TIMESTAMP()')
->getQuery();
->getResult();
throw me :
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: timestamp without time zone + integer
You can’t add an integer to a date. How should PHP/Doctrine interpret the integer ? days, hours, minutes, … ?
MySQl provides some functions to handle DATE and TIME, and some of them are available in DQL language, such as DATE_ADD which is what you need if your integer is a days amount ! Look at Doctrine documentation
And if your integer is a seconds amount, it’s not implemented in DQL so you need to create a “native SQL query” and use MySQL functions such as ADDTIME
And if you’d like to create a TIME_ADD function for DQL, you read this article that explains how to create your own DQL functions !