简体   繁体   中英

Maatwebsite Laravel Excel queued export serialization error

I am using Maatwebsite\Excel to handle data exports in my application. Some of the exports are pretty big, so I want to queue the exports. I have followed the documentation , but I get the following error when attempting to export:

You cannot serialize or unserialize PDO instances

I understand that PDO instances cannot be serialized, I just don't understand why it's telling me that since I am following what is said in the docs. Here's my code:

Controller

$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),
]);

Export

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

    //.......
}

I assume that the problem comes from using FromQuery , but I can't use FromCollection because I run out of memory since the export is so big. I need the built in chunking that FromQuery uses. Is there a way I can queue an export using FromQuery ?

You probably don't need to be setting a member variable $this->validations to that builder. That is what is ending up trying to be serialized. If you are just going to return it you don't need to store a copy on the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM