【问题标题】:You cannot serialize or unserialize PDO instances in laravel您不能在 laravel 中序列化或反序列化 PDO 实例
【发布时间】:2019-06-25 21:12:24
【问题描述】:

我正在使用像这样的默认 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 一起工作,而不是创建一个不同的工作并从同一个听众分派

【问题讨论】:

  • 你有什么问题?
  • @JonStirling 抱歉,只是有点兴奋,忘记输入上下文

标签: php laravel events serialization queue


【解决方案1】:

将项目添加到队列中将它们序列化。

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

【讨论】:

  • 谢谢你,我现在可以玩这个问题只需要正确管理我的依赖项
猜你喜欢
  • 2012-01-31
  • 2017-09-10
  • 1970-01-01
  • 2011-05-27
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-03
相关资源
最近更新 更多