Is it possible to shorten these doctrine queries?
$q_auctions = Doctrine_Query::create()
->select('COUNT(t.id) AS num_items')
->from('Auctions t')
->where('t.ends_at > NOW()');
$q_auctions_upcoming = Doctrine_Query::create()
->select('COUNT(t.id) AS num_upcoming')
->from('Auctions t')
->where('t.starts_at > NOW()');
$q_auctions_closed = Doctrine_Query::create()
->select('COUNT(t.id) AS num_closed')
->from('Auctions t')
->where('t.ends_at < NOW()');
I have these 3 very simi开发者_开发百科lar auctions in Doctrine, but I'm not sure wether I can shorten it somehow. I don't like so much repeating so maybe if someone could give me a tip.
First of all, I wouldn't worry about having the three queries too much unless you're really trying to cut down the total number of queries for this page. Indexes on starts_at and ends_at might help speed things up but depends on the number of records in those tables.
The only thing that comes to mind right now would be to do a single query, return the start and end dates, and then parse those inside a PHP foreach loop into the three groups you need. This would drop the queries from 3 to 1 but add some PHP overhead. If you've got hundreds or thousands of auctions there, this might not be a good idea.
精彩评论