【问题标题】:Maatwebsite Laravel Excel queued export serialization errorMaatwebsite Laravel Excel 排队导出序列化错误
【发布时间】:2021-05-01 13:47:44
【问题描述】:

我正在使用 Maatwebsite\Excel 来处理我的应用程序中的数据导出。一些出口相当大,所以我想排队出口。我已关注documentation,但在尝试导出时出现以下错误:

You cannot serialize or unserialize PDO instances

我知道 PDO 实例无法序列化,但我只是不明白为什么它告诉我,因为我遵循文档中所说的内容。这是我的代码:

控制器

$path = 'public/validations/' . $filename;
//the $client var is a record retrieved from a model, $input is the result of $request->all()
(new ValidationsExport($client, $input))->queue($path)->chain([
    // the script never reaches here, but $user is from \Auth::user()
    new NotifyUserOfValidationExport($user, $filename),
]);

导出

class ValidationsExport implements FromQuery, WithHeadings, WithMapping, WithStrictNullComparison, WithCustomQuerySize, WithEvents, WithColumnFormatting
{
    use Exportable;

    private $client;
    private $input;

    public function __construct($client, $input)
    {
        $this->client = $client;
        $this->input = $input;
    }

    public function query()
    {
        // this does return a query builder object, but this is required for FromQuery and is shown in the docs as an example of how to queue an export
        $this->validations = $this->getValidations();

        return $this->validations;
    }

    public function querySize(): int
    {
        $query = ....
        $size = $query->count();
        return $size;
    }

    public function headings(): array
    {
        // this is an array
        return $this->columns;
    }

    public function columnFormats(): array
    {
        return [
            'A' => NumberFormat::FORMAT_TEXT,
            'B' => NumberFormat::FORMAT_TEXT,
            'C' => NumberFormat::FORMAT_TEXT
        ];
    }

    public function map($row): array
    {
        $mapping = [];
        foreach($row as $key => $value) {
            if(is_bool($value)) {
                if($value) {
                    $mapping[$key] = "Yes";
                } else {
                    $mapping[$key] = "No";
                }
            }else{
                $mapping[$key] = $value;
            }
        }
        return $mapping;
    }

    //.......
}

我认为问题出在使用FromQuery,但我不能使用FromCollection,因为导出太大导致内存不足。我需要FromQuery 使用的内置分块。有没有办法可以使用FromQuery 对导出进行排队?

【问题讨论】:

  • 你为什么要设置$this->validations .. 如果你只是要从那个方法返回,你需要这样做吗?
  • 你是个天才@lagbox。我曾经在querySize 方法中使用它,但后来决定进行单独的查询。设置$this->validations 导致了这个问题,现在我可以看到它非常有意义。提交答案,以便我接受。谢谢!

标签: php laravel maatwebsite-excel


【解决方案1】:

您可能不需要为该构建器设置成员变量$this->validations。这就是最终试图被序列化的东西。如果您只是要退回它,则无需在课程中存储副本。

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 2019-05-20
    • 1970-01-01
    • 2020-09-04
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    相关资源
    最近更新 更多