Jump to content

Nested Selectors with Selector Arrays


thetuningspoon
 Share

Recommended Posts

Are nested selectors compatible with the new array format for selectors in PW 3? I am trying to find all pages ("Tours") that have at least one child ("Departure") that matches my conditions.

This is the only way I could get it to work (simplified for readability):

		// Build the Tour selector based on the our filters
		$selector = [];
		$selector['template'] = 'Tour';
		$selector['sort'] = 'Title';

		// Select matching Departures so that we can narrow down the Tours further
		$startDate = $this->input->get->start_date ?: date('Y-m-d'); // Never show tours without any upcoming departures scheduled
		$startDate = \DateTime::createFromFormat('Y-m-d', $startDate)->getTimestamp();

		$departureSelector = [];
		$departureSelector['template'] = 'Departure';
		if($startDate) $departureSelector['departure_date>='] = $startDate;
		$departures = $this->pages->find($departureSelector);

		$selector['children'] = $departures->getArray(); // Only include Tours with at least one matching departure

		$tours = $this->pages->find($selector); // Run our completed selector

 

I initially tried putting the $departureSelector directly into the $selector['children'] element, but this would only return a single match. Then I tried running a separate find() on the $departureSelector and putting that into $selector['children']. Same result. It wasn't until I called getArray() on $departureSelector to convert it to an array instead of an object that it worked.

I'm assuming (perhaps wrongly) that the additional find operation is less efficient than a real nested selector, so I would much rather use a nested selector. Is this a bug in the new selector arrays implementation, or am I doing something wrong?

Link to comment
Share on other sites

1 hour ago, thetuningspoon said:

Is this a bug in the new selector arrays implementation, or am I doing something wrong?

Did you try this as a non-array selector and it worked? Just wondering if there could be something in there that stops it working in any form of selector.

Link to comment
Share on other sites

4 hours ago, thetuningspoon said:

I just converted all my code to use the string format instead and it is working, so this seems like an issue specific to the array format.

I tried a few different things trying to get sub-selectors to work in the selector array format and I couldn't get it working either. Maybe someone else will chime in with a tip -  otherwise I'd say it is a bug and you should create a GitHub issue for it.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Just adding Ryan's answer from the Github issue for future reference:

Quote

The only way you can use a nested selector in a selector array is by using the a verbose selector array. Otherwise there's no way for PW to tell the difference between an array of values or a selector array. Something has to be there that says "this is another selector", and that's what the "find" property in a verbose array selector does. So what I think you are looking for is this:


$selector = array(
  array(
    'field' => 'template', 
    'value' => 'Tour'
  ),
  array(
    'field' => 'sort', 
    'value' => 'Title'
  ), 
  array(
    'field' => 'children', 
    'find' => array(
      'template' => 'departure',
      'departure_date>=' => $startDate,
    ) 
  )
);

Rather than specifying a "value", you specify a "find". The "find" can be a string selector, a verbose array selector, or a simple array selector (like we did above).

 

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