Jump to content

Exact match selector for page reference field


adrian
 Share

Recommended Posts

Hey guys - I am feeling like a PW noob asking this one. Maybe it's a slack of sleep getting this current project out the door, but I am trying to do something that I feel should be easy, but doesn't seem to be.

I have users with various possible user types. This user_types field is on the user template and is also on the "message" template, so trying to match up users with messages that are appropriate for them based on the user type they are and the message tagged for their type.

It's easy to get messages that match one of the user's types:

$pages->find("message=template, user_types={$user->user_types}")

but because users and messages can both be tagged with more than one type, I don't see any easy way to force an exact match of the user_types between the user and messages.

Separating the user_types out into an AND selector like:

user_types=type1, user_types=type2

isn't enough either because that doesn't enforce those types that aren't selected, so you also need to add like so:

user_types=type1, user_types=type2, user_types!=type3

That last one does what I want, but I feel like the page reference field getMatchQuery() method should support exact matching without having to spell it out like this.

Is there a syntax I have forgotten that does this?

Link to comment
Share on other sites

1 hour ago, adrian said:

That last one does what I want, but I feel like the page reference field getMatchQuery() method should support exact matching without having to spell it out like this.

Is there a syntax I have forgotten that does this?

That's a really good point, and a situation I haven't considered before. But I don't think there is any syntax that does what you want.

And if you look at how the values of multiple page reference fields are stored in the database (with each selected page being a separate row in the database) I can't see a way to find exact matches, either with an API selector or an SQL query, without explicitly stating all the values that you don't want to match.

So I'm thinking that one approach to make it easier to do these kinds of searches would be to create a function that:

1. Takes the field name and pages you want to match as arguments
2. Gets all the pages that have been selected in the field
3. Subtracts the pages to match from the pool of selected pages
4. Returns a selector string component for the pages not to match

Then you include that exclusion string in your selector.

I might have a play around with this later if you don't beat me to it or you come up with a smarter solution. ?

  • Like 1
Link to comment
Share on other sites

Thanks for confirming that I am not senile just yet ?

This is what I am using, where 1145 is the parent of the "user_types" page reference field.

Not terribly elegant, but it seems to work ok. I'd love to see a better solution though.

image.png.7b44d908378dc031132ff4676b083d77.png

  • Like 1
Link to comment
Share on other sites

I took a stab at a helper method and posted it here:

This makes it easier if you have a more complex definition for selectable pages than simply children, or if you later change the definition for selectable pages and don't want to have to remember to update your code, or if you have a lot of selectable pages (for the selector it only includes pages that have actually been selected somewhere).

  • Like 1
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

  • Recently Browsing   0 members

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