我正在使用这样的默认 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) {
}
}
这是我的自定义服务,我使用的是连接而不是 eloquent,每当我注入(inject)时,我都会将我的服务从事件解析到监听器并将其放入队列中,我得到错误
You cannot serialize or unserialize PDO instances
.我希望我的听众使用 implements ShouldQeueue
而不是创建不同的作业并从同一个监听器进行调度
最佳答案
将项目添加到队列中会使它们序列化。
Connection 包含一个 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);
}
}
关于php - 您不能在 laravel 中序列化或反序列化 PDO 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54477007/