adrian

Selector arrays with page reference fields with AND, not OR

Recommended Posts

Hi everyone,

In a rush and using selector arrays for the first time :) and hoping someone can point me to an easy way to convert a mult-value page reference field into an AND selector.

This:

image.png.162e6ad6f026da5f72808218395e969a.png
is getting converted into:

"parent=1031, start=1, limit=6, sort=title, category=1111, amenities.title=Kitchen|Laundry, status<1024"

but of course I want:

"parent=1031, start=1, limit=6, sort=title, category=1111, amenities.title=Kitchen, amenities.title=Laundry, status<1024"

with each selected "amenities" option expanded out so it's AND and not OR.

Anyone done this with selector array?

A little OT, and I probably need to look over some history here as to why it's not available, but sometimes I think it would be nice if we could just do:

amenities.title=Kitchen&Laundry

Thanks for any help!

  • Like 1

Share this post


Link to post
Share on other sites

Does that mean a title equals Kitchen and Laundry at the same time? How would that be possible?

Share this post


Link to post
Share on other sites
Just now, tpr said:

Does that mean a title equals Kitchen and Laundry at the same time? How would that be possible?

What I am looking for is pages that have the amenities page field with Kitchen & Laundry selected, not either, hence the AND, not OR.

Maybe my idea at the end doesn't make sense, but I am still looking to use the selector array to end up with the:

amenities.title=Kitchen, amenities.title=Laundry

which is logical.

Maybe it's not possible and I need to build up a selector string like I always have?

Share this post


Link to post
Share on other sites

I see now, but I think the & in the selector would be misleading. As for the array syntax I can say nothing as I haven't used it so far. 

  • Like 1

Share this post


Link to post
Share on other sites

Ok, I ended up looking into it and from what I can tell the only way is to use the "regular arrays" option, rather than the associative approach.

This seems to work:

foreach($input->get->amenities as $amenity) {
      $selector[] = ['amenities.title', $amenity];
}

It gets converted to:

image.png.7cb77b4df78191103867fc0ae78a90c5.png

and the final selector ends up having this, which is what I need.

amenities.title=Kitchen, amenities.title=Laundry

Maybe this is the only way, but if anyone else has another solution, let me know. Otherwise, hope this helps someone else down the road,

  • Like 6

Share this post


Link to post
Share on other sites
15 hours ago, adrian said:

This:

image.png.162e6ad6f026da5f72808218395e969a.png
is getting converted into:

You didn't show us your original query code, just a Tracy output. How did your code look like?

 

Share this post


Link to post
Share on other sites
2 hours ago, dragan said:

You didn't show us your original query code, just a Tracy output. How did your code look like?

$selector['amenities.title'] = $input->get->amenities;

It's not surprising it results in what it did, but I'd still like an associative array approach that worked.

Share this post


Link to post
Share on other sites
27 minutes ago, noelboss said:

You could probably implode the array with 


, amenities.title= as glue.

Thanks @noelboss but not sure how that would work with an associative array because you need to reproduce "amenities.titles" for each value which would just override the previous one. But maybe I am misunderstanding your point?

Share this post


Link to post
Share on other sites

Shouldn't it work like so;

$amenities = implode(', amenities.title=', $yourArray); // starts with a comma
// should result in ", amenities.title=$array[0], amenities.title=$array[1], amenities.title=$array[2]" …
$pages->find("parent=1031, start=1, limit=6, sort=title, category=1111 {$amenities}, status<1024");

I'm looking at the debug output where you have the array with the two titles… Or am I missing the point?

Share this post


Link to post
Share on other sites

@noelboss - I think the issue that you might be missing is that I am using the selector array approach: https://processwire.com/blog/posts/processwire-3.0.13-selector-upgrades-and-new-form-builder-version/#selector-engine-array-support

There is no issue doing what you are suggesting with the regular selector string approach, but the array approach is quite nice when you're building up a complex selector from user input.

  • Like 1

Share this post


Link to post
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


  • Recently Browsing   0 members

    No registered users viewing this page.