Jump to content

Selector arrays with OR


Ferdi Derksen
 Share

Recommended Posts

Hello,

I'm trying to build a selector array to work with an OR operator.

I need all pages where the datetime field ("date_pub_end") is empty or in the future. In a selector string this could be solved with the following two examples (with and without OR-group)

$selector1 = "... ,!date_pub_end<={$now}, ...";

$selector2 = "... ,(date_pub_end=''),(date_pub_end>={$now}), ...";

But I'd like it to work in selector arrays, so far I couldn't find a solution.
 

Link to comment
Share on other sites

I think the associative array option you may use either 'group' or 'or' to specify or-group, but i can't figure out how to do it, as the blog post that announced the feature doesn't explain how to do groups in selector arrays... do hope to find out how to do it soon...

https://processwire.com/blog/posts/processwire-3.0.13-selector-upgrades-and-new-form-builder-version/#building-a-selector-string-with-user-input-example

 

Link to comment
Share on other sites

cool, in that case it should be easy, you just use that exact syntax, but add the 'group' key:

$selector = array(
  array(
    'field' => 'date_pub_end', 
    'operator' => '<=',
    'value' => $now,
  ), 
  array(
    'field' => 'date_pub_end',
    'operator' => '=' ,
    'value' => '',
    'group' => 'foo'
  ),
  array(
    'field' => 'date_pub_end',
    'operator' => '>=',
    'value' => $now,
    'group' => 'foo' 
   ),
);

 

i theeenk

Edited by Macrura
updated to something i think is more correct...
  • Like 2
Link to comment
Share on other sites

Well in this case I (kinda) solved my issue using an array like this;

$selector[] = ['field' => 'date_pub_end', 'operator' => '<=', 'value' => $now, 'not' => true];

I also read the documentation about the array-selectors in the blog and from Ryan but I guess we still have to wait a little bit for the upgraded documentation for this announcement;

Quote

Array selectors also support OR-groups and sub-selectors, which we'll cover in more detail soon in the Selectors documentation for ProcessWire 3.x.

 

Tnx anyways @Macrura @kongondo for pointing me in the right direction!

  • Like 2
Link to comment
Share on other sites

  • 3 months later...
On 10.2.2017 at 6:02 PM, Macrura said:

cool, in that case it should be easy, you just use that exact syntax, but add the 'group' key:


$selector = array(
  array(
    'field' => 'date_pub_end', 
    'operator' => '<=',
    'value' => $now,
  ), 
  array(
    'field' => 'date_pub_end',
    'operator' => '=' ,
    'value' => '',
    'group' => 'foo'
  ),
  array(
    'field' => 'date_pub_end',
    'operator' => '>=',
    'value' => $now,
    'group' => 'foo' 
   ),
);

 

i theeenk

Has anyone actually got that to work? It keeps throwing following error for me:

<b>Fatal error</b>:  Exception: Unknown Selector operator: '' -- was your selector value properly escaped? field='A.S.', value='', selector: 'A.S.' (in ~/Documents/PW-Testumgebung/wire/core/Selectors.php line 357)

Here's the exact selector I'm using:

$selector = [
    [
        'field' => 'title',
        'value' => 'A.S.',
        'operator' => '^=',
        'group' => 'foo',
    ],
    [
        'field' => 'title',
        'value' => 'Adam',
        'operator' => '^=',
        'group' => 'foo',
    ],
];

 

Edit:

I think that was a bad example. At least the operator should be different. What seems to be working thought is the following code:

$selector = [
    [
        'title^=' => 'A.S.',
        'title=' => 'Adam Blenk'
    ]
];

Unfortunately now I'm forced to use associative arrays. I prefer the regular array syntax a lot. Is there any other way? Please enlight us @ryan

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