MWSimpleAdminCrudBundle导出过滤结果失效求助
Hey there, I’ve dealt with this exact issue before using MWSimpleAdminCrudBundle, so let me walk you through how to get your exports respecting the filtered results:
Step 1: Track down the export logic
First, find the action in DefaultController that handles exports (it’s usually named something like exportAction or split into separate methods for PDF/XLS/JSON). Right now, that method is probably spinning up a fresh QueryBuilder without pulling in the filter data stored in your session.
Step 2: Pull filter data from the session
The bundle stores your filter criteria in the session using a key that looks like admin_crud_filter_[YourEntityName]. Grab that data with:
$filterSessionKey = sprintf('admin_crud_filter_%s', $this->entity); $filterData = $this->get('session')->get($filterSessionKey);
Double-check that $this->entity is set correctly (the bundle initializes this in the controller for your entity, so it should be good to go).
Step 3: Apply filters to your export query
Look at the list action (around line 89 you referenced) — it uses the bundle’s filter service to modify the query builder with the session filters. You need to copy that logic into your export action.
Here’s how to do it:
// Start with your base query builder $queryBuilder = $this->getDoctrine() ->getRepository($this->entity) ->createQueryBuilder('a'); // Apply filters if we have them stored if (!empty($filterData)) { // Get the bundle's filter handler service $filterHandler = $this->get('mwsimple_admin_crud.filter.handler'); // Apply the session filters to your query builder $filterHandler->applyFilters($queryBuilder, $filterData, $this->entity); }
If the list action doesn’t use a service and instead builds the filtered query manually, just replicate that conditional logic (adding WHERE clauses based on the filter data) in your export method.
Step 4: Use the filtered query for exports
Replace the original query the export was using (like findAll()) with the results from your filtered builder:
$entities = $queryBuilder->getQuery()->getResult();
Now your export will only include the entities that match your filter.
Step 5: Debug to confirm
- After setting a filter, dump the
$filterDatato make sure the session is storing your criteria correctly. - Dump the DQL from the query builder (
dump($queryBuilder->getDQL());) to verify the filter conditions are being added to the query.
One last thing: Make sure the session key matches exactly what the bundle uses in the list action. Some minor version differences might tweak the naming, so cross-check that if things still aren’t working.
内容的提问来源于stack exchange,提问作者Karel Attl

