是否可以缩短这些学说查询?
$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()');
我在《教义》中有这 3 个非常相似的拍卖,但我不确定是否可以以某种方式缩短它。我不喜欢重复太多,所以也许有人可以给我提示。
$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 similar 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我不会担心这三个查询太多,除非您真的想减少此页面的查询总数。 starts_at 和ends_at 上的索引可能有助于加快速度,但取决于这些表中的记录数。
现在想到的唯一一件事是执行单个查询,返回开始日期和结束日期,然后将 PHP foreach 循环中的日期解析为您需要的三组。这会将查询次数从 3 个减少到 1 个,但会增加一些 PHP 开销。如果您在那里有数百或数千场拍卖,这可能不是一个好主意。
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.