Illuminate/Queue与Illuminate/Database参数绑定异常问题排查
问题:脱离Laravel框架使用Illuminate Queue数据库驱动时SQL语法错误
环境与依赖
- PHP版本:7.4(曾尝试升级至8.1)
- MySQL版本:8.0
- 引入的Composer包:
"illuminate/queue": "^8.83", "illuminate/bus": "^8.83", "illuminate/contracts": "^8.83"
注:曾尝试升级到Laravel 9版本对应包,问题依然存在
队列初始化配置
已完成队列表的迁移创建,手动初始化QueueManager的代码如下:
<?php $queue = new Queue; $queue->addConnection([ 'driver' => 'database', 'table' => ee('db')->dbprefix . 'dgq_jobs', 'queue' => 'default', 'retry_after' => 90, 'after_commit' => false, ]); $databaseConfig = $provider->make('DatabaseConfig'); $queue->addConnector('database', function () use ($databaseConfig) { $pdo = new PDO( sprintf('mysql:host=%s; dbname=%s', $databaseConfig['host'], $databaseConfig['database']), $databaseConfig['username'], $databaseConfig['password'] ); $connection = new Connection($pdo); $connectionResolver = new ConnectionResolver(['default' => $connection]); $connectionResolver->setDefaultConnection('default'); return new DatabaseConnector($connectionResolver); }); return $queue->getQueueManager();
触发问题的代码与报错
执行以下任务推送代码时:
/** @var \Illuminate\Queue\QueueManager $queue */ $queue->push('test', ['foo' => 'bar']);
出现SQL语法错误,参数绑定失效,reserved_at对应的?未被正确处理,报错信息如下:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near "\"exp_dgq_jobs\" (\"queue\", \"attempts\", \"reserved_at\", \"available_at\", \"created_at\"' at line 1 (SQL: insert into \"exp_dgq_jobs\" (\"queue\", \"attempts\", \"reserved_at\", \"available_at\", \"created_at\", \"payload\") values (default, 0, ?, 1674567590, 1674567590, {"uuid":"6bf7a17e-dda3-4fed-903a-8714e5a2d146","displayName":"test","job":"test","maxTries":null,"maxExceptions":null,"failOnTimeout":false,"backoff":null,"timeout":null,"data":{"foo":"bar"}}))
调试情况
通过调试发现,buildDatabaseRecord方法已明确将reserved_at设为null,但Connection->prepareBindings()方法未正确处理该null值,最终导致SQL语句格式错误。
请问我遗漏了哪些配置或步骤?
内容的提问来源于stack exchange,提问作者Brian Litzinger
相关产品推荐
相关产品推荐

