Jump to content

Most selector operators throwing errors


dragan
 Share

Recommended Posts

I've created a couple pages that hold nothing more than categories - referenced by portfolio pages. The multi-select page fieldtypes work just fine.

Now I'd like to find all pages that have category X. Since most items have more than one category, I need selector operators like %= or *=. But these simply don't work.

e.g.

$cat = $sanitizer->selectorValue($page->title); // works

// none of these work:
$p = $pages->find("portfolio_categories*=$cat, template=portfolio, sort=-Project_Date");
$p = $pages->find("portfolio_categories%=$cat, template=portfolio, sort=-Project_Date");
$p = $pages->find("portfolio_categories*=$cat");

produce the error messages I've uploaded as attachment below. Do I have a buggy mySQL version installed? Other basic selector queries (foo=bar) work fine...

post-1325-0-74078300-1368258549_thumb.pn

post-1325-0-42025600-1368258550_thumb.pn

Link to comment
Share on other sites

If portfolio_categories is a Page field, you can't and don't need to use '*=' or '%=', use '=' instead.

In the case of Page fields '=' checks if the value of said Page field contains specified page, not if it's equal to it, so you should use that and a page ID (or Page object) to see if one of selected categories is the one you're looking for:

// this should work:
$cat = $sanitizer->selectorValue($page->title);
$cat_page = $pages->get("template=portfolio_category, name=$cat");
$p = $pages->find("portfolio_categories=$cat_page");

// .. though in this case you should do this:
$p = $pages->find("portfolio_categories=$page");

// .. and you could even do this:
$p = $pages->find("portfolio_categories={$page->id}";
  • Like 2
Link to comment
Share on other sites

Thanks!

In the end, this worked:

$p = $pages->find("portfolio_categories=$page, template=portfolio, sort=-Project_Date");

foreach($p as $pp) {
	echo "<a href='{$pp->url}'>{$pp->title}</a><br>";
}

I'm still a bit puzzled why the other method didn't work. So is $page some sort of short form for $page->title? Guess it must be, because it wouldn't work otherwise. On the other hand, for these category pages I'm only using the title field, nothing else.

Link to comment
Share on other sites

$cat = $sanitizer->selectorValue($page->title); // works
$p = $pages->find("portfolio_categories*=$cat, template=portfolio, sort=-Project_Date"); 

This doesn't work because a page field doesn't work with operators and titles... as teppo explained.

So you could imageine this:

portfolio_categories*=mycategory

could be seen same as:

1003|1004|1005*="mycategory"

So you would need to give it a page id or a page array and PW will make the rest.

portfolio_categories=$somecategorypage

If you do echo $somecategorypage, the toString() method of page will output the id...

So it would be like this now:

1003|1004|1005=1004

Or if you use a page array.

echo $pageArray it will return a 1003|1004|1005 string which is usable also in a selector.

1003|1004|1005=1003|1004|1005
  • Like 2
Link to comment
Share on other sites

OK, thanks for the explanations. The big choice of field-types is a bit overwhelming, and as a newbie, I'm still learning...

(btw, I think it's a bit of a misnomer to call a variable pageArray, when it's not an actual array, but a string. Perhaps pageList would be more appropriate?)

Link to comment
Share on other sites

...but here it actually is an array, thus the name  :).

When PHP sees we're dealing with strings the string representation of a variable is used. In this case the trigger is an unescaped dollar sign between double quotation marks, but it could be the concatenation operator '.' as well. To get such a representation, the __toString() method of the class (PageArray in this case) is automagically called. See http://www.php.net/manual/en/language.oop5.magic.php#object.tostring.

The __toString() method has been implemented to return a single page id (Page or PageArray with one item) or a list of page id's with a '|' (pipe) as a separator.

I was trying to clarify the situation but I'm not sure if that's what happened after all :D.

  • Like 2
Link to comment
Share on other sites

// .. and you could even do this:
$p = $pages->find("portfolio_categories={$page->id}";

<diversion>

Oh my word! I didn't know you could pass variables like this "portfolio_categories={$page->id}" to selectors!! I have been defining a variable first, e.g. $category= $page->title. Then passing $title to the selector "category=$category"

</diversion>

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...