php - 您不能在 laravel 中序列化或反序列化 PDO 实例
问题描述
我正在使用这样的默认 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,每当我注入时,我都会将我的服务从事件解析到侦听器并将其放入队列中,我得到错误You cannot serialize or unserialize PDO instances
。我希望我的听众一起工作,implements ShouldQeueue
而不是创建不同的工作并从同一个听众分派
解决方案
将项目添加到队列中会使它们序列化。
Connection 包含一个 PDO 实例,但您无法序列化 PDO 实例,因此,您会收到该错误。
您应该实现__sleep 和 __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);
}
}
推荐阅读
- symfony - Fos elastica在代码表中获取代码名称
- physx - 如何在 UE4 中为 PhysX 定义 World Bound?
- jquery - 如何在jquery中的小数点后面自动填充0(零)?
- javascript - 循环遍历以特定字符串开头的 ID
- cakephp - cakephp 3: bake 添加了不需要的规则 "$rules->isUnique(['email'])"
- hadoop3 - hadoop3 无法创建时间线服务器 hbase 表
- ios - 设计一个自定义视图,用于多个 ViewController 的
- java - Java 10:Axis 1.4 SOAP 服务——WSDD 仍然可能吗?
- c++ - 如何设置从 C++ 控制台应用程序到 C++ COM DLL 的回调
- python - 使用 in 运算符匹配元组中的项目