我正在使用像这样的默认laravel事件系统
use \Illuminate\Database\Connection;
class ExampleService {
private $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
}
class ExampleEvent {
private $service;
public function __construc(ExampleService $service) {
$this->service = $service;
}
}
class ExampleListener implements ShouldQueue {
public function handle(ExampleEvent $event) {
}
}
这是我的自定义服务,我在其中使用连接而不是雄辩,每当我注入时,我就将事件从服务解析为侦听器并将其放在队列中,我将得到错误You cannot serialize or unserialize PDO instances
。我希望我的监听器与implements ShouldQeueue
一起使用,而不是创建一个不同的作业并从同一个监听器进行调度
答案 0 :(得分:2)
将项目添加到队列会对其进行序列化。
连接包含一个PDO实例,但是您无法序列化PDO实例,因此,您会收到该错误。
您应该实现__sleep and __wakeup方法以确保序列化正确进行,例如:
class ExampleService {
private $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public function __sleep() {
return []; //Pass the names of the variables that should be serialised here
}
public function __wakeup() {
//Since we can't serialize the connection we need to re-open it when we unserialise
$this->connection = app()->make(Connection::class);
}
}