adrian Posted April 13, 2018 Share Posted April 13, 2018 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: 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! 1 Link to comment Share on other sites More sharing options...
tpr Posted April 13, 2018 Share Posted April 13, 2018 Does that mean a title equals Kitchen and Laundry at the same time? How would that be possible? Link to comment Share on other sites More sharing options...
adrian Posted April 13, 2018 Author Share Posted April 13, 2018 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? Link to comment Share on other sites More sharing options...
tpr Posted April 13, 2018 Share Posted April 13, 2018 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. 1 Link to comment Share on other sites More sharing options...
adrian Posted April 13, 2018 Author Share Posted April 13, 2018 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: 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, 6 Link to comment Share on other sites More sharing options...
dragan Posted April 14, 2018 Share Posted April 14, 2018 15 hours ago, adrian said: This: is getting converted into: You didn't show us your original query code, just a Tracy output. How did your code look like? Link to comment Share on other sites More sharing options...
adrian Posted April 14, 2018 Author Share Posted April 14, 2018 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. Link to comment Share on other sites More sharing options...
Noel Boss Posted April 17, 2018 Share Posted April 17, 2018 You could probably implode the array with , amenities.title= as glue. Link to comment Share on other sites More sharing options...
adrian Posted April 17, 2018 Author Share Posted April 17, 2018 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? Link to comment Share on other sites More sharing options...
Noel Boss Posted April 17, 2018 Share Posted April 17, 2018 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? Link to comment Share on other sites More sharing options...
adrian Posted April 17, 2018 Author Share Posted April 17, 2018 @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. 1 Link to comment Share on other sites More sharing options...
adrian Posted January 30, 2019 Author Share Posted January 30, 2019 Just a follow up in case anyone is interested. On a separate project I needed to get into grouped OR selectors and I decided the easiest way is to create a standard array and simply do an implode on it, eg: $selector = []; $selector[] = 'user_types='.$u->user_type; $selector[] = 'ages=(ages.count='.$pages->count('template=age').'), ages=(ages='.$u->age).')'; $selector[] = 'sexes=(sexes.count='.$pages->count('template=sex').'), sexes=(sexes='.$u->sex.')'; $selector = implode(', ', $selector); This is back to how I used to do things and I feel like this is almost as clean as the selector array approach and so much more flexible. 3 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