[5.x] How to order an Entry query by a field within a Matrix field embedded on the entry #15747
rob-c-baker
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
So this, so far, is how I'm going about this. Technically it works and the important bits happen in the database, which is what I was after due to scalability / pagination requirements. If anyone can see any better ways of doing this, please jump in!
The matrix field on the entry contains several things but we are focussing on the
dateFrom
field within.Add a new date field on the entry that will be used for ordering the Entry query, we added a condition so it is only visible to admins which in effect should make it invisible to other content editors. The new field has the handle
mostRecentDateFrom
.Add an event to a custom module that populates that entry before save.
The field is a date field with the handle
dateFrom
on a Matrix field with the handledates
on the Entry:CAST()
clause needed for a virtual column by doing an entry query using the new field:Pick out the ORDER BY clause, in our case it was:
elements_sites
DB table that pulls themostRecentDateFrom
field out of the entry's JSON blob into a column in our case calledrallyStartDate
. Use the clause picked out above.orderBy()
this new virtual field, hopefully / eventually using the index just made:Note: As yet I have not convinced the MySQL engine to use this new index using this query - not certain how much it will help at this point, working on that.
Any comments / suggestions / improvements are welcome, or indeed a way to do this that is a bit more "built in" rather than the "round the houses" approach I have got to here.
Beta Was this translation helpful? Give feedback.
All reactions