Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Techniques on how to export large CSV file with Symfony

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!

like image 619
iamjc015 Avatar asked Dec 11 '22 06:12

iamjc015


1 Answers

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"');
like image 84
G1.3 Avatar answered Dec 28 '22 23:12

G1.3