I’m trying to build what I think are relatively simple queries using pdoResources / tvFilters.
The sort of thing I want to achieve would be described as:
(groups contains "Teachers" OR groups contains "All Groups")
AND
(local-authorities contains "Glasgow City" OR local-authorities contains "Scotland-wide")
You probably have to create your own &where condition instead of using the &tvFilters property (or alternatively duplicate the getResources snippet and change the code to process AND first).
Something like this might work:
&where=`["EXISTS (SELECT 1 FROM `modx_site_tmplvars` t INNER JOIN `modx_site_tmplvar_contentvalues` tc ON tc.tmplvarid = t.id WHERE t.name = 'groups' AND tc.contentid = `modResource`.id AND (tc.value LIKE '%Teachers%' OR tc.value LIKE '%All Groups%')) AND EXISTS (SELECT 1 FROM `modx_site_tmplvars` t INNER JOIN `modx_site_tmplvar_contentvalues` tc ON tc.tmplvarid = t.id WHERE t.name = 'local-authorities' AND tc.contentid = `modResource`.id AND (tc.value LIKE '%Glasgow City%' OR tc.value LIKE '%Scotland%'))"]`
The code behind this has been in place for a while and works well - I’ve only just been asked to add the OR conditions in now - hence have reached this point.
Any advice on how the syntax would work to add your suggested code as additional AND conditions to this?
I have tried the following:
&where=`[{ "template:IN" : [ 11 ] },{ "parent:IN" : [ 116 ] },{EXISTS (SELECT 1 FROM `modx_site_tmplvars` t INNER JOIN `modx_site_tmplvar_contentvalues` tc ON tc.tmplvarid = t.id WHERE t.name = "groups" AND tc.contentid = `modResource`.id AND (tc.value LIKE "%Teachers%" OR tc.value LIKE "%All Groups%"))}]`
but the added condition seems to be ignored and has no effect on the number of results returned [it should].
I think the subquery has to be a string "EXISTS ..." and not an object {EXISTS ...}.
I tested it with pdoResources and it only works if the property &includeTVs is empty.
If the TVs are included, then the code tries to be clever and replaces every value of the TV-name in the where-condition with the proper query value (groups becomes `TVgroups`.`value` ). This can help to simplify the where condition like in this example:
[[pdoResources?
...
&includeTVs=`groups,local-authorities`
&where=`[{"template:IN":[11]},{"parent:IN":[116]},"(groups LIKE '%Teachers%' OR groups LIKE '%All Groups%') AND (local-authorities LIKE '%Glasgow City%' OR local-authorities LIKE '%Scotland%')"]`
]]
But the problem is, that the code replaces groups in %All Groups% as well. You’d have to rename the TV to make it work with pdoResources.
Also, setting the property &showLog=`1` and then checking the SQL query can be very helpful to analyze why something doesn’t work.
With getResources I think you have to use this ugly EXISTS subquery (and it only works if the TVs have no default value).
[[!getResources?
...
&where=`[{"template:IN":[20]},{"parent:IN":[1]},"EXISTS (SELECT 1 FROM `modx_site_tmplvars` t INNER JOIN `modx_site_tmplvar_contentvalues` tc ON tc.tmplvarid = t.id WHERE t.name = 'groups' AND tc.contentid = `modResource`.id AND (tc.value LIKE '%Teachers%' OR tc.value LIKE '%All Groups%')) AND EXISTS (SELECT 1 FROM `modx_site_tmplvars` t INNER JOIN `modx_site_tmplvar_contentvalues` tc ON tc.tmplvarid = t.id WHERE t.name = 'local-authorities' AND tc.contentid = `modResource`.id AND (tc.value LIKE '%Glasgow City%' OR tc.value LIKE '%Scotland%'))"]`
]]
This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.