laravel 查询参数与illuminate/database和illuminate/queue的绑定问题

vbkedwbf  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(197)

我在CMS的一个插件中使用Laravel应用程序外的Illuminate\Queue。所以Laravel或Illuminate的唯一示例是我需要的这些包:

"illuminate/queue": "^8.83",
"illuminate/bus": "^8.83",
"illuminate/contracts": "^8.83"

我首先尝试使用队列的数据库作为默认驱动程序,因为CMS是数据库驱动的,然后为SQS等提供选项。我已经设置了一切,以便迁移创建我的队列表,当我进行以下调用以将某些内容推送到队列时,一切似乎都连接在一起。

/** @var \Illuminate\Queue\QueueManager $queue */
$queue->push('test', ['foo' => 'bar']);

然后它以下面的错误结束,参数绑定不起作用,它在值列表中留下了?。

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"}}))

我已经对整个请求进行了单步调试,感觉像是一个bug,但这是我第一次使用Laravel或它的某个包,所以也许我遗漏了什么?这个函数显式地将reserved_at设置为null,Connection-〉prepareBindings()方法不对?做任何事情,它只是将它保留为那个值,所以查询失败。

protected function buildDatabaseRecord($queue, $payload, $availableAt, $attempts = 0)
    {
        return ['queue' => $queue, 'attempts' => $attempts, 'reserved_at' => null, 'available_at' => $availableAt, 'created_at' => $this->currentTime(), 'payload' => $payload];
    }

我错过了什么?在我看来一切都很好,我有点不知所措。我正在用PHP 7.4做这个(暂时)。也许我会尝试8.1,看看它是否改变了Illuminate包。也使用MySQL 8。
更新:错误发生前的潜在相关屏幕截图。
更新2:我尝试了PHP8.1和最新的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();
a11xaf1n

a11xaf1n1#

我能够重现你看到的错误。我没有深入研究,但我认为这可能是由于PDO对象没有完全按照Illuminate Queue库的预期设置连接。
使用Illuminate\Database库创建连接的修改解决了我的测试环境中的问题:

$database = new \Illuminate\Database\Capsule\Manager;
$queue = new \Illuminate\Queue\Capsule\Manager;

$database->addConnection([
    'driver' => 'mysql',
    'host' => 'localhost',
    'database' => 'db_name',
    'username' => 'username',
    'password' => '',
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
]);

$queue->addConnector('database', function () use ($database) {
    $connection = $database->getConnection();
    $connectionResolver = new \Illuminate\Database\ConnectionResolver(['default' => $connection]);
    $connectionResolver->setDefaultConnection('default');

    return new \Illuminate\Queue\Connectors\DatabaseConnector($connectionResolver);
});

$queue->addConnection([
    'driver' => 'database',
    'table' => 'jobs_table',
    'queue' => 'default',
    'retry_after' => 90,
    'after_commit' => false,
]);

$queue->getQueueManager()->push('SendEmail', ['message' => 'test']);

相关问题