php - 在 Laravel 观察者中使用自定义 for 循环
问题描述
我想在发生更新事件时开始倒计时,并根据 laravel 5.8 API 应用程序中的某些条件更新其他字段。
基本上,这些是我想要完成的步骤
当订单表
orderStatus
字段更新为 时processing
,开始倒计时 12 分钟。在倒计时期间,如果订单表
paymentStatus
字段更新自pending
,则停止倒计时并更新orderStatus
至completed
。在倒计时结束时,如果订单
paymentStatus
仍处于待处理状态,请更新orderStatus
至completed
和paymentStatus
至aborted
。
为了实现这一点,我制作了一个 OrderObserver 来监听更新事件并尝试了这段代码
public function updated(Order $order)
{
if ($order->paymentStatus == 'paid') {
$order->update(['orderStatus' => 'completed']);
}
if ($order->paymentStatus == 'processing') {
// start a timeout function
for ($orderedAt = $order->created_at; $orderedAt <= $orderedAt->addMinutes(12); $orderedAt->addSecond()) {
if ($order->paymentStatus !== 'pending') {
$order->update(['orderStatus' => 'completed']);
break;
}
if ($orderedAt == $orderedAt->addMinutes(12)) {
$order->update(['orderStatus' => 'completed', 'paymentStatus' => 'aborted']);
break;
}
}
}
}
显然,第一个 if 块工作正常,但第二个没有。从 created_at 时间开始 12 分钟后,两者orderStatus
都paymentStatus
不会更新。
我觉得 for 循环是错误的,但我还没有找到正确的方法。
如何使用 OrderObserver 更新事件实现上述步骤?
解决方案
I think you are misunderstand what addMinutes
method does.
created_at
attribute is a Carbon object, isn't it? If so, addMinutes
just calculate a sum of time and change the value of the attribute.
And addSecond
does the same, but with seconds.
If you want to have a long-running script you would have to use a sleep call to stop the process for some seconds.
However, the best solution IMO, you should create a cronb job and check every minute if some order is in pending
status for 12 minutes, and than change the status.
Take a look at https://laravel.com/docs/5.8/scheduling
推荐阅读
- google-chrome - Chrome 打开 iframe 中的每个网页
- python - TypeError:列表索引必须是整数或切片,而不是代码的 str
- python-2.x - 如何使用 ROS 和 unittest 导入 python 脚本
- r - 错误:$ 运算符对于使用 ggplot2 的原子向量无效
- python - 为什么这个 python 代码会产生副作用?
- python - 如何通过查看比较运算符对列表进行排名?
- python - 如何在Google or-tools中设置车辆最大行驶距离和GlobalSpanCostCoefficient
- c++ - DirectX 11 引擎在第三次代码改进后没有突然运行,我不知道为什么
- python - 在 Python 或 SQL 中使用或条件的唯一标识符
- python - 为什么每次进行新查找时,Selenium 都会抛出“StaleElementReference”异常?