Jump to content

Recommended Posts

Posted

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? 

Posted (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 by kongondo
  • Like 1
Posted

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)

Posted (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 by kongondo
Posted

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 :).

Posted

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?

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...