androbey Posted March 4, 2023 Share Posted March 4, 2023 Hello all, once again I have a special request. Also this time I am not sure if there is a better "ProcessWire typical" solution. I want to find exactly one page where the value of a page reference matches the value I am looking for. So far so good: $pages->get("template=store_template,equipment_ref.title='Equipment2') returns "Store-Page 1" (see sample structure below). However, I need the consideration for special cases, when a certain equipment is referenced in multiple "store_templates". If this case occurs, the "correct" store page should be selected based on another condition. E.g. if the condition is "Type1" and the search is for Equipment1, "Store-Page 2" should be returned. If the condition is "Type1" but the search is for Equipment2, "Store-Page 1" should still be returned. However, the different types are text values (not numeric values). So I guess a regular sort parameter on the selector won't work. Is there any chance to solve this without using loops? Simplified structure: Store-Page 1(store_template) - Equipment1 (page ref to equipment_template) - equipment2 - Equipment3 Store page 2 - Equipment1 - Equipment4 Link to comment Share on other sites More sharing options...
gebeer Posted March 5, 2023 Share Posted March 5, 2023 Where is the condition Type1 in your structure? Link to comment Share on other sites More sharing options...
androbey Posted March 5, 2023 Author Share Posted March 5, 2023 Condition is/should be attached to store_template pages. So one store could have one entry "Type1" and another store could have multiple types ("Type2", "Type3"). There's no field yet for it, but it could be anything (simple text field, multi select field, page ref...). The condition to search for "Type1" comes from external at runtime, of course. But the more I think about it, the less likely it seems to be a good solution for database level search. Link to comment Share on other sites More sharing options...
gebeer Posted March 5, 2023 Share Posted March 5, 2023 If it is a field called "type" in store_template, it should be as simple as // for text field or select options $pages->get("template=store_template,equipment_ref.title='Equipment2, type="Type1"'); // for page ref field $pages->get("template=store_template,equipment_ref.title='Equipment2, type.title="Type1"'); Even if you have a page reference field with multiple entries, this selector will return a page as long as one of the entries has title "Type1". Link to comment Share on other sites More sharing options...
androbey Posted March 5, 2023 Author Share Posted March 5, 2023 Thank you for your suggestion. That would be a ideal solution if there would be a 1 to 1 connection. But unfortunately it's more complex. //would return null (because Store2 has "Type1", but Store2 has no "Equipment2"), so should return Store1 (where Equipment1 is available) $pages->get("template=store_template,equipment_ref.title='Equipment2', type.title='Type1'); Nevermind, probably was a silly request in first place. It works with loops and performance loss is minimal (only small data set). 1 Link to comment Share on other sites More sharing options...
Jan Romero Posted March 5, 2023 Share Posted March 5, 2023 Maybe show those loops you already have, because I for one am unable to figure out what’s going on here in the first place? 1 hour ago, androbey said: (where Equipment1 is available) I suspect you mean Equipment2 here? So matching the equipment is kind of a fallback in case equipment AND type can’t both be matched? Link to comment Share on other sites More sharing options...
androbey Posted March 5, 2023 Author Share Posted March 5, 2023 Yes, good spot, you're right. Should be "Equipment2". And you're absolutely right, matching the equipment (without matching both type and equipment) is the fallback. With a little restructuring (above is only simplified structure), I now just use two selectors (better than a loop which checks if there are pages matching a type). //Are there any stores which both match equipment and title? $pages->get("template=store_template, equipment_ref.title='Equipment2', type.title='Type1'"); //If no: "Fallback" selector.. $pages->get("template=store_template, equipment_ref.title='Equipment2'"); Link to comment Share on other sites More sharing options...
Jan Romero Posted March 5, 2023 Share Posted March 5, 2023 Have you tried Or-Groups? https://processwire.com/docs/selectors/#or-groups $pages->get("template=store_template, (equipment_ref.title='Equipment2', type.title='Type1'), (equipment_ref.title='Equipment2')"); I think this should work for you? 1 Link to comment Share on other sites More sharing options...
androbey Posted March 5, 2023 Author Share Posted March 5, 2023 Thank you for your suggestion. I have tried to use an or-group. This also does not work, as the ordering of the or condition in the selector has no effect. So if I use the or group, the selector would return the first page where either condition is met (even if type is not Type1). So there is no weighting (at least I did not find any). Link to comment Share on other sites More sharing options...
Jan Romero Posted March 5, 2023 Share Posted March 5, 2023 Ah yes, my bad, you would have to make the groups mutually exclusive by adding the condition “does not have Type1” to the second one. Unfortunately I don’t think this is possible with FieldtypeMulti fields… 1 Link to comment Share on other sites More sharing options...
androbey Posted March 5, 2023 Author Share Posted March 5, 2023 No, thank you for your help! Much appreciated! 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