20.
CSV Export
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
16 Comments
Hi,
I'm learning to use EasyAdmin 3 (on Symfony 5) and I'm trying to use the CSV export. In this part of the tutorial there is the follow code:
`
$queryBuilder = $this->createListQueryBuilder(
`
Unfortunately, with the last version of Doctrine, I don't have createListQueryBuilder method.
Could you explain me how to replace it? I didn't understand so well.
Thanks for your help
Hey Gaetano S.
The
createListQueryBuilderis not a Doctrine method, it's a EasyAdmin method which creates the query builder for the "List" view. I believe the new name for that method iscreateIndexQueryBuilder()Cheers!
Well,
it is not so easy for me to understand this method. I mean, I'm not sure about the parameters to pass it. This is my use case:
I would like to create a custom button on EDIT page on StudyCrudController to extract a CSV with the data of ResultStudy, so I added this code for my button:
`
public function configureActions(Actions $actions): Actions
$exportCSV = Action::new('export', 'Export CSV', 'fa fa-file-export')
->setLabel('field.export')
->addCssClass('btn btn-primary')
->linkToCrudAction('exportAction');
{
$sortDirection = $adminContext->getRequest()->query->get('sortDirection');
if (empty($sortDirection) || !in_array(strtoupper($sortDirection), ['ASC', 'DESC'])) {
}
// here for this method you mentioned, the parameters are not the same (SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters)
?????,
?????,
?????,
?????
);
return $exporter->getResponseFromQueryBuilder(
`
Thank you so much for your help
Seems like things have changed even more than I expected but you can see an example of how to call the
createIndexQueryBuilder()method https://github.com/EasyCorp/EasyAdminBundle/blob/master/src/Controller/AbstractCrudController.php#L116the key is on using the
AdminContext $contextargumentOk, thanks for the example. I'll try.
Thanks, I will try it.
I have followed this example and it's working great, thank you! My question is when I have filtered results (not dql_filter) on a list page is there a way to export the filtered result to a csv file? Filters from the request query is an object, how might I pass along to limit my results in my csv to match that of my filtered results?
Thank you in advance.
You can use:
$this->filterQueryBuilder($queryBuilder);Hey Brent
I think what you can do is to override the
createQueryBuilder()method of your export CSV action and add the same filters you are applying to the list view. Does it makes sense?Cheers!
i'm getting Call to a member function getQuery() on null
because of the $queryBuilder
Hey Khaoula A.
That's strange :)
When you do this call inside your controller's action
The
$queryBuilderis set tonull?I will need to see how you are creating it
Cheers!
Hello,
how to define dynamic columns in the "getColumnsForEntity" function ?
Thanks for your help
Hey Remy J.
It depends on what you want to achieve but probably you could implement your own way of generating columns by using the PropertyAccess component or maybe even the Serializer component.
Cheers!
Have you any plans to add CSV importing to this tutorial, it would be great to see how this can be done...
To remove the export button on each line, I just don't add this
list:
actions: ['export']
And it works the same, my exportAction in the controller is still called.
Also you can remove this line from the twig view
{#{% if easyadmin_action_is_enabled_for_list_view('export', _entity_config.name) %}#}
Thx for this tutorial it saved me time !
Hey Kaizoku
Thanks for sharing your findings, as you can see there are too many ways of doing things in EasyAdminBundle :)
Cheers!
"Houston: no signs of life"
Start the conversation!