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.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeWhat else could we possibly configure with the list view? How about sorting, or filtering list via DQL. OoooOOooo.
Configuring Sort
First, sorting... which we get for free. Already, the genuses are sorted by id, but we can click any column to sort by that. But this isn't sticky: when you come back to the genus list page, it's back to filtering by id.
Sorting by name would be a bit more awesome. And you can probably guess what the config looks like to do this. Under Genus
and list
, add sort: name
:
// ... lines 1 - 80 | |
easy_admin: | |
// ... lines 82 - 89 | |
entities: | |
Genus: | |
// ... lines 92 - 94 | |
list: | |
// ... lines 96 - 106 | |
sort: 'name' | |
// ... lines 108 - 136 |
This is the new default field for sorting.
Sorting via Relations
Oh, but we can get fancier. Under GenusNote
, what if I told you I wanted to sort by the name of the Genus
it's related to? Yea, that would mean sorting across a relation. But that's totally possible: sort: ['genus.name', 'ASC']
:
// ... lines 1 - 80 | |
easy_admin: | |
// ... lines 82 - 89 | |
entities: | |
// ... lines 91 - 110 | |
GenusNote: | |
// ... lines 112 - 113 | |
list: | |
// ... lines 115 - 121 | |
sort: ['genus.name', 'ASC'] | |
// ... lines 123 - 137 |
This also controls the direction. It sorts descending by default.
Try it! Nice! This works... just don't get too confident and try to do this across multiple relationships... that's not going to work.
Disabling Sort Fields
The ability to sort via any field with no setup is great! Though... sometimes it doesn't make sense - like with the "User avatar" field. To tighten things up, you can disable sorting. Find that field's list
config and add a new option at the end: sortable: false
:
// ... lines 1 - 80 | |
easy_admin: | |
// ... lines 82 - 89 | |
entities: | |
// ... lines 91 - 110 | |
GenusNote: | |
// ... lines 112 - 113 | |
list: | |
// ... line 115 | |
fields: | |
// ... lines 117 - 118 | |
- { property: 'userAvatarFilename', label: 'User avatar', type: 'image', base_path: '/images/', sortable: false } | |
// ... lines 120 - 137 |
And... gone!
DQL Filtering
Ok, let's turn to something fun: DQL filtering. Like, what if we want to hide some genuses entirely from the list and search page?
But first, so far, it seems like we're limited to one entity section per entity. That's a lie! Let me show you: add a new section under entities
called GenusHorde
- I just made that up. Below, set its class to AppBundle\Entity\Genus
:
// ... lines 1 - 80 | |
easy_admin: | |
// ... lines 82 - 89 | |
entities: | |
// ... lines 91 - 110 | |
GenusHorde: | |
class: AppBundle\Entity\Genus |
You see, some scientists are worried that certain genuses are becoming too large... and threaten the survival of mankind. They want a new GenusHorde
section where they can keep track of all of the genuses that have a lot of species. It's scary stuff, so we'll add a label: HORDE of Genuses
with a scary icon:
// ... lines 1 - 80 | |
easy_admin: | |
// ... lines 82 - 89 | |
entities: | |
// ... lines 91 - 110 | |
GenusHorde: | |
class: AppBundle\Entity\Genus | |
label: HORDE of Genuses ? !!! | |
// ... lines 114 - 140 |
Tip
Fun fact! You can press Control
+Command
+Space
to open up the icon menu on a Mac.
And all of a sudden... ah! We have a new "Horde of Genuses" section! Run!!!
Of course, this still shows all genuses. I want to filter this to only list genuses that have a lot of species. Start by adding a list
key and a new, awe-inspiring option: dql_filter
. For the value, pretend that you're building a query in Doctrine. So, entity.speciedCount >= 50000
:
// ... lines 1 - 80 | |
easy_admin: | |
// ... lines 82 - 89 | |
entities: | |
// ... lines 91 - 110 | |
GenusHorde: | |
// ... lines 112 - 113 | |
list: | |
dql_filter: 'entity.speciesCount >= 50000' | |
// ... lines 116 - 142 |
The alias will always be entity
.
Try it! Ten down to... only 7 menacing genuses!
And just like any query, you can get more complex. How about: AND entity.isPublished = true
:
// ... lines 1 - 80 | |
easy_admin: | |
// ... lines 82 - 89 | |
entities: | |
// ... lines 91 - 110 | |
GenusHorde: | |
// ... lines 112 - 113 | |
list: | |
dql_filter: 'entity.speciesCount >= 50000 AND entity.isPublished = true' | |
// ... lines 116 - 144 |
And to really focus on the genuses that are certain to overtake humanity, sort it by speciesCount
and give the section a helpful message: Run for your life!!!
Add scary icons for emphasis:
// ... lines 1 - 80 | |
easy_admin: | |
// ... lines 82 - 89 | |
entities: | |
// ... lines 91 - 110 | |
GenusHorde: | |
// ... lines 112 - 113 | |
list: | |
dql_filter: 'entity.speciesCount >= 50000 AND entity.isPublished = true' | |
sort: 'speciesCount' | |
help: Run for your life!!! ??? | |
// ... lines 118 - 144 |
Ok... refresh! Ah... now only three genuses are threatening mankind.
Oh, and search automatically re-uses the dql_filter
from list: these are 2 results from the possible 3. And like always, you can override this. Under search
, set the dql_filter
to the same value, but without the isPublished
check:
// ... lines 1 - 80 | |
easy_admin: | |
// ... lines 82 - 89 | |
entities: | |
// ... lines 91 - 110 | |
GenusHorde: | |
// ... lines 112 - 117 | |
search: | |
dql_filter: 'entity.speciesCount >= 50000' | |
// ... lines 120 - 146 |
Try that. Boom! 3 more genuses that - when published - will spell certain doom for all.
Next! We'll save humanity by learning how to override the many templates that EasyAdminBundle uses.
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.