如何解决Maatwebsite Laravel Excel 排队导出序列化错误
我使用 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
。这就是最终试图序列化的内容。如果您只是要返回它,则无需在类中存储副本。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。