I have a task to export 100,000 rows of records from my database to csv format.
What are the best approaches on doing this? I am totally clueless on what to search and study.
I want to have chunk downloads like the other sites with PART format while the download is not yet finished and at the same time do not let my server be exhausted.
How can I do that?
Thanks!
You can use the StreamedResponse from Symfony HttpFoundation component combined with Doctrine iterate.
Something like this:
$response = new StreamedResponse(function () {
$data = $this->myQuery()->iterate();
$csv = fopen('php://output', 'w+');
while (false !== ($line = $data->next())) {
fputcsv($csv, [$line[0]->column1], ';');
}
fclose($csv);
});
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
$response->headers->set('Content-Disposition', 'attachment; filename="file.csv"');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With