Jump to content

find() by property or similar way to combine recurring selectors?


thomas
 Share

Recommended Posts

Hello,

I need to build a combination of selectors which need to be in almost every find() statement on my site. Being lazy by nature, I was hoping for a way to make my life easier. I thought of three ways:

- Hook after find, then find again with the new selectors. This works, but it scares me. Maybe it's not as bad performance wise as I think but my guess is, it is.

- Build a property by which to find(). I liked this idea until I found out it doesn't work. The idea was to addHookProperty to Page, and then include this into find. But find only likes fields so no luck.

- Hook before find and modify the argument passed to find(). I couldn't get this to work: 

$event->setArgument(0,"mySelector==yes,".$event->arguments[0]);
$event->return = $event->arguments;

What am I doing wrong? What are your ways of managing silly recurring selectors? Any thoughts on my ideas?

Thanks!

thomas

Link to comment
Share on other sites

I don't think I ever had something like this, but I imagine I'd do it with small module;

// module
class MySelect {
  public function __construct(){
    //
  }
  public function findFiltered($selector){
    return wire('pages')->find('limit=1,'.$selector);
  }

}

// template
$mySelect = $modules->get('MySelect');
$pages = $mySelect->findFiltered('template=subselection');
Edited by adamkiss
Fixed the code to something what should really work, duh
Link to comment
Share on other sites

$event->setArgument(0,"mySelector==yes,".$event->arguments[0]);
$event->return = $event->arguments;

I'm not sure that I totally understand the example, and think I'd need to see the rest of the class to get the right picture. But there is no "==" operator in selectors, so that's one potential problem. Another is that you are assigning $event->return = $event->arguments. Why -- I don't understand this? :) 

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