07.
DQL Filtering & Sorting
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.
6 Comments
Hi,
I have an entity with two date fields (when entered, due date). Does dql_filter support filtering a date field (eg. whenentered >= now)? I'm having trouble finding an example of this. Thank you in advance.
Hey Brent !
Excellent question. Hmm, that is a bit tricky. In a normal query, I would do something like
andWhere('whenEntered > :now')->setParameter('now', new \DateTime()), but we can't do that inside of YAML. So, we'll need to apply the dql_filter manually (not in YAML). There are a few ways to do this, but the one I'd use is an event:1) Add an event listener (https://symfonycasts.com/screencast/easyadminbundle/event-hooks) on an event called EasyAdminEvents::POST_LIST_QUERY_BUILDER (which is the string
easy_admin.post_list_query_builder).2) Your listener function will be passed a
GenericEventobject, but with aquery_builderargument on it that you can mess with - something like this:I haven't tested this, but that should do the trick :). Let me know if you have any issues. You might also want to add another event listener on the
EasyAdminEvents::POST_LIST_QUERY_BUILDERevent.Cheers!
Hi team !
I need to add an advance search feature. Ex filter the list view by sub-family.
I don't think I've seen such feature in this course (maybe I'm missing something?).
So I checked the doc, but I didn't found anything except this : https://github.com/javiereg...
but this is only a proposal.
Do you have any idea how to implement this kind of filter ?
Best regards.
Yo Kaizoku!
Hmm. How would your search field work exactly? I mean, would I type in the name of the sub-family and it would find all Genus that are related to that Sub-Family? Or would you have some sort of drop-down to select the sub-family? Regardless of what you need exactly, keep in mind that the
searchActionis just an action that creates a custom paginator and then renders the list template: https://github.com/javiereguiluz/EasyAdminBundle/blob/aac1732adc3ac1401dc08dca756ea19708a248af/src/Controller/AdminController.php#L347. So, in theory, I think you could override this, create your own paginator by using whatever logic you want, and then render the same template :).Cheers!
[edit : remove code part as this isn't easy to read. If someone want code example send me an email.]
Thank you Ryan for the suggestion.
Here is how I choose to implement a dynamic DQL filter.
As suggested I overrided the search template
This will give you 2 drop down select box along the search input.
Now about the controller.
The filters I need are specific to only one entity and it should apply to the list and search view.
So I went for the "Customization Based on Entity Controllers" option.
https://symfony.com/doc/current/bundles/EasyAdminBundle/book/complex-dynamic-backends.html#customization-based-on-entity-controllers
I created a specific controller and override the list and search action.
In both method I added a form with the filters I needed.
And the trick is to use the dql_filter parameter provided in the searchAction.
It's the same parameter that is hardcoded in the config file in this video, but this time it's changed by the user.
It works very well.
The only problem is that it's not possible to make complex filter action as according to the doc you can put only WHERE condition in the $dql_filter parameter.
For instance I needed a filter based on a JOIN and I couldn't find how to do this.
I think this is a very usefull case - Backend dynamic DQL Filter - and maybe it can be added in a futur video ;)
Here is how the controler looks like.
Nice work! And thanks for sharing (and even using the code blocks from Disqus - woo!). I agree on the DQL filtering limitation - it's a bummer. Currently , there are a bunch of little things like this in EasyAdminBundle - Javier (the maintainer) is balancing staying simple... which means not solving *every* problem. I get it - it's but yea, when you find a feature you want that's not there, it's no fun :).
Cheers!
"Houston: no signs of life"
Start the conversation!