在 Doctrine ORM 中,如何将秒数求和到时间戳字段?
我有一个 starts_at
字段,用于保存事件开始时的时间戳。另外,我还有另一个字段 seconds_long
表示事件持续的秒数。
我需要选择所有未完成的事件,但我就是找不到对 starts_at
和 seconds_long
字段求和的方法。我首先尝试对它们进行总结,期望 Doctrine 能发挥一些魔力,但它没有起作用:
addWhere("c.starts_at + c.seconds_long > ?", date('Y-m-d H:i:s', time()));
不幸的是,这不起作用。然后,我尝试将 starts_at
字段转换为 UNIX 秒,但我没有找到在 sqlite 和 MySQL 中工作的 SQL 函数。
有办法做到这一点吗?如何?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能有一种更简单的方法可以做到这一点,但是将您拥有的字段设置为“starts_at”和“ends_at”怎么样 - 两者都作为时间戳。
然后,您可以通过减法得到差值:starts_at -ends_at > ?。
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 > ?.
这应该有效,但我没有测试它。
在你的教义记录课上
This should work but I didn't test it.
In your Doctrine Record class
我认为不可能创建一个同时适用于 SQLite 和 MySQL 的查询。 ORM 可以使很多东西独立于数据库,但无法将您的任务同时转换为 SQLite 和 MySQL。
你应该首先简化任务的想法。在 PHP 中做一些事情来防止您必须按照您现在的方式构建查询;或更改数据库结构。
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.