views:

418

answers:

3

I have a starts_at field that keeps a timestamp with the moment something starts. Also, I have another field seconds_long that represents the amount of seconds that the event lasts.

I need to select all unfinished events but I just can't find a way to sum the starts_at and seconds_long fields. I first tried to sum them, expecting Doctrine do some magic, but it didn't work:

addWhere("c.starts_at + c.seconds_long > ?", date('Y-m-d H:i:s', time()));

Unfortunately, that does not work. Then, I tried to convert the starts_at field to UNIX seconds, but I didn't find a SQL function that worked in sqlite and MySQL.

Is there a way to do this? How?

+1  A: 

There's probably a simpler way to do this, but what about setting the fields you have as "starts_at" and "ends_at" - both as timestamps.

You could then get the difference with a subtraction: starts_at - ends_at > ?.

Tom
Yes, I know I could do that. Also, I coud save starts_at in UNIX seconds. I will do that if I can't find a better solution, but I wanted to avoid changing the model... There should be an easy way to do this!
miguelSantirso
Another way that might work... something like: addWhere("c.starts_at < ?", date('Y-m-d H:i:s', strtotime('+5 seconds'))); ... this would compare "starts_at" to "NOW + 5 seconds". You could then substitute various elements for variables as needed. Note the "smaller than" sign in the Doctrine query.
Tom
Or possibly even something MySQL-based like this: addWhere("c.starts_at + SEC_TO_TIME(c.seconds_long) > ?", ...) but not sure what Doctrine will accept and what not. There's quite a lot of possible options here: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Tom
I use sqlite, so unfortunately I don't have that MySQL functions... I think I will ask in the Doctrine forums to see if they can solve this problem.
miguelSantirso
A: 

This should work but I didn't test it.

In your Doctrine Record class

function findFinishedOccurances()
{
    $this->getTable()-
         ->createQuery()
         ->select("c.*, DATE_ADD(c.starts_at, INTERVAL c.seconds_long SECOND) AS c.finished_at")
         ->where("c.finished_at >= ?", date('Y-m-d H:i:s', time()))
         ->execute(); 
}
Travis
Doesn't seem to work :( I did this doctrine query to test it:php symfony doctrine:dql "select c.starts_at, c.seconds_long, c.starts_at + c.seconds_long as finishes_at from Competition c"And I get the following output, which proves that the query is not working:- id: '1' starts_at: '2010-03-02 12:28:50' seconds_long: '180' finishes_at: '2010-03-02 12:28:50'- id: '2' starts_at: '2010-03-03 10:33:45' seconds_long: '1800' finishes_at: '2010-03-03 10:33:45'...
miguelSantirso
Ok, I think I see your problem now .... check the updated query.
Travis
Actually, that won't work ... I just noticed you wanted it to work with both sqlite and mysql.Here's a thought though. When are you setting the seconds_long field? In theory, the only way you know if an event is finished is if you know how long it lasted, therefore you should be able to calculate what's finished by simply checking if the seconds_long field is null.
Travis
The seconds long are set when the event is created so checking NULL wont work. Nah, I will just save the starts_at field as UNIX seconds.
miguelSantirso
A: 

I think it is not possible to make a query which works with both SQLite and MySQL. An ORM can make lots of stuff database independend but there's no way to translate your task to both SQLite and MySQL.

What you should do its first simplify the idea of the task. Do something in PHP to prevent you from having to build the query the way you are doing; or change the Database structure.

douwe