Zeka Posted February 2, 2017 Share Posted February 2, 2017 Hi. I just found out some strange behavior in selectors. There are two snippets of code with small difference in order of selectors. First example returns all pages which match "has_parent" selector even if they are not "blog-post" template (not expected result) , but the second one returns only "blog-post" pages ( expected result) . $categories = $page->children("template=blog")->add($page); $items = $pages->find("template=blog-post, has_parent={$categories}, limit=7"); $categories = $page->children("template=blog")->add($page); $items = $pages->find("has_parent={$categories}, template=blog-post, limit=7"); Is there any documentation on how order of selectors affect the results? Link to comment Share on other sites More sharing options...
kongondo Posted February 2, 2017 Share Posted February 2, 2017 (edited) FWIW, here are the resultant queries (similar, not identical to yours) for the two finds: Find 1: $categories = $page->children("template=blog")->add($page); $selector = "template=blog-post, has_parent={$categories}, limit=7"; $query = wire('pages')->getPageFinder()->find(new Selectors($selector), array('returnQuery' => true))->getQuery(); echo $query; Resultant Query: SELECT SQL_CALC_FOUND_ROWS pages.id,pages.parent_id,pages.templates_id FROM `pages` WHERE (pages.templates_id=77) AND pages.parent_id IN (SELECT pages_id FROM pages_parents WHERE parents_id=3545 OR pages_id=3545) OR pages.parent_id IN (SELECT pages_id FROM pages_parents WHERE parents_id=3560 OR pages_id=3560) OR pages.parent_id IN (SELECT pages_id FROM pages_parents WHERE parents_id=3565 OR pages_id=3565) OR pages.parent_id IN (SELECT pages_id FROM pages_parents WHERE parents_id=1044 OR pages_id=1044) AND (pages.status<1024) GROUP BY pages.id LIMIT 0,7 Find 2: $categories = $page->children("template=blog")->add($page); $selector = "has_parent={$categories}, template=blog-post, limit=7"; $query = wire('pages')->getPageFinder()->find(new Selectors($selector), array('returnQuery' => true))->getQuery(); echo $query; Resultant Query: SELECT SQL_CALC_FOUND_ROWS pages.id,pages.parent_id,pages.templates_id FROM `pages` WHERE pages.parent_id IN (SELECT pages_id FROM pages_parents WHERE parents_id=3545 OR pages_id=3545) OR pages.parent_id IN (SELECT pages_id FROM pages_parents WHERE parents_id=3560 OR pages_id=3560) OR pages.parent_id IN (SELECT pages_id FROM pages_parents WHERE parents_id=3565 OR pages_id=3565) OR pages.parent_id IN (SELECT pages_id FROM pages_parents WHERE parents_id=1044 OR pages_id=1044) AND (pages.templates_id=77) AND (pages.status<1024) GROUP BY pages.id LIMIT 0,7 I am guessing (am no SQL guru) the key thing is that we are dealing with an AND selector, maybe? Edit: Me thinks this is a bug in the selector engine itself? Edited February 2, 2017 by kongondo 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 2, 2017 Share Posted February 2, 2017 has_parent does not support multiple alternative values with pipes. Only "hackish" version is to use OR groups like so: (has_parent=$cat1), (has_parent=$cat2) Link to comment Share on other sites More sharing options...
kongondo Posted February 2, 2017 Share Posted February 2, 2017 (edited) Actually @LostKobrakai, it does as of ProcessWire 2.5.22? No? https://processwire.com/blog/posts/processwire-core-and-profields-updates-2.5.22/#has_parent-selectors-now-support-multi-value Edited February 2, 2017 by kongondo Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 2, 2017 Share Posted February 2, 2017 I'm not 100% sure about 3.0, but I've a 2.7 installation, where this caused me so much trouble I know for a fact that it doesn't work. 1 Link to comment Share on other sites More sharing options...
kongondo Posted February 2, 2017 Share Posted February 2, 2017 (edited) You are right, Benjamin. If one changes the selector to match only 1 parent like shown below, the two finds become equivalents. // these two are equivalents $items = $pages->find("template=minimal, has_parent={$page}, limit=10"); $items = $pages->find("has_parent={$page}, template=minimal, limit=10"); Do you know if a bug report has been filed about this? Edited February 2, 2017 by kongondo Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 2, 2017 Share Posted February 2, 2017 Had to search hard, but found it: https://github.com/ryancramerdesign/ProcessWire/issues/1232 Edit: And the new one: https://github.com/processwire/processwire-requests/issues/17 1 Link to comment Share on other sites More sharing options...
kongondo Posted February 2, 2017 Share Posted February 2, 2017 Excellent! Have upvoted the new one. We will also need to update the Docs. The selectors docs says it doesn't work; the blog post says it does . Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 2, 2017 Share Posted February 2, 2017 I really feel this should be fixed, especially as the OR group syntax does work. Link to comment Share on other sites More sharing options...
kongondo Posted February 2, 2017 Share Posted February 2, 2017 Maybe we should file it as a bug...(which it is, since it has been implemented since 2.5.22 but it's not working), rather than a request? Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 2, 2017 Share Posted February 2, 2017 Yeah, wasn't to confident as it has been around a long time as issue on the old repo Link to comment Share on other sites More sharing options...
kongondo Posted February 2, 2017 Share Posted February 2, 2017 True, but not sure Ryan is monitoring issues in the old repo . Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 4, 2017 Share Posted February 4, 2017 Can someone of you guys test out the new commit of Ryan? My project with that issue is currently stuck on 2.7. Link to comment Share on other sites More sharing options...
Zeka Posted February 4, 2017 Author Share Posted February 4, 2017 I just tested it with the new commit and it works as expected. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now