Jump to content

Selector with multiple Templates, select on specific template field value


Harmster
 Share

Recommended Posts

Hey hey

Now my title might seem confusing but this is essentially what i have:

$items = $pages->find("template=match|notification, summoner_id=$summoner_string, sort=-create_date, limit=15");

And I want a field called notification_type to be equal to a certain amount of values, now what I first thought would work would be

notification.notification_type = something

but it doesn't

The other template doesn't have the field notification_type so I cant just put it in there

Any help?

thanks

Link to comment
Share on other sites

I don't think that's possible with a single find as you'd need an or-selector like this (this is a pseudo selector, not an actual one): "template=match OR (template=notification AND notification_type=something), ...". One way is to use two find() calls and merge the resulting PageArrays but that isn't necessarily an option as you obviously need to sort and limit in the database (or at least you're doing it now).

There is a trick you could probably use to get over the limitation in this case. This is clearly a hack so you have been warned =).

  • create a field 'fake' of type integer and add it to the template 'match'
  • make sure every page with template 'match' has the same value for field 'fake' ('-1' for example). Choose a value that never exists in field 'notification_type' (you'd need an autoload module just for filling the field 'fake'...)
  • use a selector like this: "template=match|notification, notification_type|fake=something|-1, summoner_id=$summoner_string, sort=-create_date, limit=15"

Now that's one hell of a dirty saturday night trick isn't it? But it works. ;) I truly hope someone comes up with a better alternative. Or that you could live without those sort and limit selectors and use two finds instead of one.

*** Edit ***

Actually I just found a bit cleaner way, one without a fake field. Assuming field 'notification_type' can't have value of 'match' you can use this selector:

"template=match|notification, template|notification_type=match|something, summoner_id=$summoner_string, sort=-create_date, limit=15" 

It's a trick as well, but not a dirty one anymore. :)

Do note that there seems to be a limitation with builtin fields (or something else weird): you have to use 'template|notification_type', as using them the other way around results in an error "Field does not exists: template". I'll experiment on this a bit more and file an issue if it seems to be a bug.

  • Like 1
Link to comment
Share on other sites

Soma, looks like it depends on the first field of the selector: if that one is native then other native fields work as well ("name|custom_x|template=..." works). But if the first field is custom, then only custom fields may follow ("custom_x|name|template=..." does not work).

There's a todo comment in PageFinder.php regarding native fields not being multi-field aware (https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/core/PageFinder.php#L215), so it's technically not a bug but a known missing feature. It's even halfway done, just needs fields in certain order. ;)

  • 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...