SonataAdminBundle: Filtering by sub entity properties causes unexpected results in global search
The configureDatagridFilters function of the MembershipAdmin class is configured using fields from a linked entity User (1/n : a Membership belongs to one User, a User may have several memberships) :
$datagridMapper
->add('user.username')
->add('user.lastname')
->add('user.email')
->add('category')
->add('amount');
This works fine in the MembershipAdmin filters, and list can be filtered using these fields.
Problem occurs when using the global search feature, which is based on the datagridFilters : when searching for any word, all related entities are queried using this word in the ‘where’ clause, except Memberhip which is queried without any ‘where’ clause. Consequently, the ‘Search Results’ page shows all records of the related Membership table as results.
When commenting out the 3 ‘user.*’ fields, the global search behaviour is as expected and returns only accurate results base on a correct ‘where’ clause.
traces:
[2016-04-22 10:09:23] request.INFO: Matched route “sonata_admin_search”. {“route_parameters”:{“controller":"Sonata\AdminBundle\Controller\CoreController::searchAction”,"route":“sonata_admin_search”},“request_uri”:“https://escmp/admin/search?q=mytest”} [] … [2016-04-22 10:09:26] doctrine.DEBUG: SELECT count(DISTINCT m0.id) AS sclr_0 FROM Membership m0 LEFT JOIN User u1_ ON m0_.Member_id = u1_.id LEFT JOIN MembershipCategory m2_ ON m0_.category_id = m2_.id [] []
Bundle version : 2.3.10 | Symfony version : 2.7.11 | php version : 5.6.2
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 1
- Comments: 16 (9 by maintainers)
you got it right for the problem, as soon as I add an indirect field to the filter, the global search fails as it doesn’t apply any filter in the query. For the root cause, I’m afraid you’ve got it wrong : the
$filter->getOption('global_search', false)returns true for the indirect fields, I’ve checked this in a debug session