kalibora / csv-streamed-response
CSV streamed response that saves memory usage with symfony and doctrine
Installs: 2 067
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.1.0
- doctrine/orm: ^2.6
- goodby/csv: ^1.3
- kalibora/chunk-generator: ~0.4
- symfony/http-foundation: ^4.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpstan/phpstan: ^0.11.19
- phpstan/phpstan-strict-rules: ^0.11.1
- phpunit/phpunit: ^7.5
README
CSV streamed response that saves memory usage with symfony and doctrine.
Usage
example 1: Specify columns with DQL
class DefaultController extends Controller { public function csvAction(UserRepository $repository) { return CsvStreamedResponse::builder() ->setRowsFromDoctrineQueryBuilder( $repository->createQueryBuilder('u')->select('u.id, u.name') ) ->build() ; } }
example 2: Specify columns with entity methods
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() ; } }
example 3: Fetch join a to-many collection in QueryBuilder
Suppress error for Iterate with fetch join in class ...
.
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() ; } }