Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

php - Maatwebsite Laravel Excel queued export serialization error

I am using MaatwebsiteExcel 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?

question from:https://stackoverflow.com/questions/65924011/maatwebsite-laravel-excel-queued-export-serialization-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...