Jump to content

Extend and search "Selectors" object


pwFoo
 Share

Recommended Posts

I played with the Selectors object and would like to use it for a special use case...

That is a custom Selectors object with dummy data converted to an php array to see the structure (Selectors object is a WireArray with "fields" added)

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [field] => seg1
                    [value] => val1
                    [not] => 
                    [group] => 
                    [quote] => 
                    [forceMatch] => 
                )
            [1] => Array
                (
                    [field] => seg2
                    [value] => val2
                    [not] => 
                    [group] => 
                    [quote] => 
                    [forceMatch] => 
                )
        )
)

 

But instead of "seg1" the field name is "field" with value "seg1" (= my field name).

 

So I can't search the Selectors WireArray (= custom WireArray with added Selectors objects) with PW "find('seg1=val1')", Is there a way to search with "find()" or build a simple wrapper to make the elements searchable / filterable with find()?

Link to comment
Share on other sites

52 minutes ago, pwFoo said:

custom Selectors object with dummy data

To clearify. It is a PW Selectors object (not a custom class!) with dummy data.

Because I need a selector (object) and some custom additional properties. Would be nice to just use the Selectors object with added properties searchable with find.

 

And a missing part here.

I tried to add custom properties to Selectors object, but get an error message:

  • $this->property = 'value'; // ignored
  • $this->set('property', 'value'); // error see below
  • $this->addHookProperty(...); // error see below
Error: Exception: Item 'selector' set to ProcessWire\ApiRoute is not an allowed type (in C:\xampp\htdocs\pw\wire\core\WireArray.php line 450)

 

Link to comment
Share on other sites

Is there a way to redirect properties / fields to make them searchable by find($selector)? For example redirect a call to "$obj->prop<X>" to a function and return the needed value? Would that help to get find('prop1=myValue') work?

 

I tried addHookProperty, but isn't usable for find().

$wa = new WireArray();
$obj1 = new Selectors('seg1=val1,seg2=val2');
$obj1->addhookProperty('seg1', function($event){
    $event->return = $event->object->getSelectorByField('seg1');
});
echo $obj1->seg1; // "seg1=val1"
$wa->add($obj1);
$output = $wa->find('seg1=val1');   // object with items "count 0"

 

Link to comment
Share on other sites

On 1/2/2019 at 11:46 PM, pwFoo said:

So I can't search the Selectors WireArray (= custom WireArray with added Selectors objects) with PW "find('seg1=val1')", Is there a way to search with "find()" or build a simple wrapper to make the elements searchable / filterable with find()?

It's not fully clear to me what you're asking, but you can use find() to get individual Selector objects from a Selectors object by both field and value.

2019-01-05_191030.thumb.png.fc410bfcbb74f7aa239a732c88da54f3.png

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

×
×
  • Create New...