且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数据库查询生成器有时会返回数组,而不是将对象作为排队作业运行

更新时间:2023-02-19 18:56:54

从5.4版开始,Laravel 不再通过 config/database.php 支持PDO提取模式的配置.默认情况下,框架将获取模式设置为PDO::FETCH_OBJ,尽管我们可以通过监听StatementPrepared事件来覆盖此设置:

Since version 5.4, Laravel no longer supports configuration of the PDO fetch mode through config/database.php. By default, the framework sets the fetch mode to PDO::FETCH_OBJ, though we can override this setting by listening for the StatementPrepared event:

Event::listen(StatementPrepared::class, function ($event) {
    $event->statement->setFetchMode(PDO::FETCH_ASSOC);
});

似乎某个排队的作业订阅了此事件并更改了获取模式.如果我们使用queue:work Artisan控制台命令启动队列工作器,则侦听器会继续执行所有后续作业 ,因为此命令会为 all 引导应用程序一次. em>工作者处理的工作.这可以解释为什么重新启动工作程序可以暂时解决此问题.

It seems possible that a certain queued job subscribes to this event and changes the fetch mode. If we start a queue worker using the queue:work Artisan console command, the listener lingers for any subsequent jobs because this command boots the application once for all the jobs that that the worker processes. This would explain why restarting the worker fixes the issue temporarily.

因此,更改获取模式的作业必须在完成或失败后将其重新设置.每当更改作业的任何全局应用程序状态时,我们都需要格外小心.

For this reason, jobs that change the fetch mode must set it back after completion or failure. We need to exercise the same care whenever we change any global application state from a job.