PHP code example of kalibora / csv-streamed-response

1. Go to this page and download the library: Download kalibora/csv-streamed-response library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kalibora / csv-streamed-response example snippets


class DefaultController extends Controller
{
    public function csvAction(UserRepository $repository)
    {
        return CsvStreamedResponse::builder()
            ->setRowsFromDoctrineQueryBuilder(
                $repository->createQueryBuilder('u')->select('u.id, u.name')
            )
            ->build()
        ;
    }
}

class DefaultController extends Controller
{
    public function csvAction(UserRepository $repository)
    {
        return CsvStreamedResponse::builder()
            ->setRowsFromDoctrineQueryBuilder(
                $repository->createQueryBuilder('u'),
                function ($user) {
                    return [
                        $user->getId(),
                        $user->getName(),
                    ];
                }
            )
            ->setCsvColumnHeaders([
                'user_id',
                'user_name',
            ])
            ->build()
        ;
    }
}

class DefaultController extends Controller
{
    public function csvAction(UserRepository $repository)
    {
        $qb = $repository->createQueryBuilder('u')
            ->leftJoin('u.emails', 'em') // Join to-many collection
        ;

        return CsvStreamedResponse::builder()
            ->setRowsFromDoctrineQueryBuilder(
                $qb,
                function ($user) {
                    return [
                        $user->getId(),
                        $user->getName(),
                        implode(',', $user->getEmails()),
                    ];
                },
                $fetchJoinCollection = true
            )
            ->setCsvColumnHeaders([
                'user_id',
                'user_name',
                'emails',
            ])
            ->build()
        ;
    }
}