pdoResources pagination and record count inconsistency with complex filters
0 reputation · 05 May 2024, 11:28 UTC
0 reputation · 05 May 2024, 11:28 UTC
When managing large resource datasets in MODX Revolution, pdoResources is used to bound query results through limit and offset parameters. This prevents memory exhaustion by restricting the number of records processed per page, often in conjunction with pdoPage to automate offset calculations.
A specific challenge arises when applying complex filters across deeply nested categories. While the limit parameter correctly bounds the result set for the current view, there is uncertainty regarding the accuracy of the total record count returned for pagination logic when these hierarchical filters are active.
The total count reported by pdoResources should reflect only the filtered subset of resources, not the entire category tree. pdoPage calculates offsets from that count, so limit bounds the visible rows while pagination stays consistent only when the count query and data query apply identical filters and return the same number of distinct resources.
pdoResources issues a data query with LIMIT/OFFSET and a separate COUNT(*) query without them.&where, &parents, &depth, &class, and related parameters as the data query.pdoPage reads the totalVar placeholder, defaulting to total, to build page links.COUNT queries differently than newer releases.Assumption: you are running MODX Revolution with pdoTools, and the mismatch appears when &includeTVs, custom joins, or deep &parents/&depth filters are active.
COUNT(*) counts every joined row, inflating the total beyond the number of distinct resources.DISTINCT on the resource id. Without DISTINCT modResource.id in the count query, multiplied rows overstate the total. Newer pdoTools versions attempt this automatically, but complex &where JSON or TV filters can bypass it.&parents with &depth may generate derived tables; if the count query builds those conditions differently than the data query, totals drift.&cacheTime can serve an old total after resources are added or removed.&distinct=1 to the pdoResources call so DISTINCT modResource.id applies to both queries.&where, &tvFilters, or joins at runtime between the count and data phases.&tvFilters or an explicit &leftJoin with a &where clause on the TV table instead of relying on &includeTVs=1 alone.&limit and compare the visible distinct resources against [[+total]], or enable &countOutput=1 and output [[+total]] on the first page.Enable SQL logging for one request and compare the generated SELECT COUNT(*) statement against the data query. Confirm both use the same WHERE clauses and that the count query includes DISTINCT on the resource primary key. Run this in a non-production or low-traffic context to limit logging overhead.
Does the generated count query contain DISTINCT modResource.id, and what are its exact JOIN clauses? That detail determines whether &distinct=1 alone resolves the mismatch or whether a version upgrade or manual count snippet is required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.