Jump to content

Recommended Posts

Posted

I know this seems fairly basic, but I'm a bit stumped. I have a Selector field that returns something like "template=198" in response to the user selecting a template (i.e. it returns the id, not the name). Another field on the same page selects a parent page, say "parent=20940". If I use $pages->find("parent=20940, template=198") then the correct pages are found. If I do 

$subPages = $pages->find('parent=20940');
d($subPages);
$subPages = $subPages->find('template=198');
d($subPages);

then the second dump is an empty array. This can be fixed by using 'template.id=198' in the second selector. However, at least in my context, it is not really feasible to hack the selector that is returned from the Selector field. I have no idea what the selector might be.

(BTW, the context, in case it is relevant, is that I am iterating the selector test down the page tree, starting at the parent page's children and stopping at the first level that finds a match).

Any ideas?

Posted

I think you've just come across another one of those cases where in-memory selectors don't match DB ones 😖

  • Like 2
Posted
12 hours ago, adrian said:

another one of those cases where in-memory selectors don't match DB ones

AKA 'bugs'? Are these documented somewhere. I'm wondering if it is possible to apply a sanitizer hook to fix them.

Posted
1 minute ago, MarkE said:

AKA 'bugs'?

Not a bug but rather the result of the two evolving separately without being designed to be "equal" from the beginning. One afterthought followed the other. We must live with it.

Posted
13 hours ago, MarkE said:

This can be fixed by using 'template.id=198' in the second selector.

What stops you from using that? I must be missing the point, I guess...

Posted
8 hours ago, szabesz said:

I must be missing the point, I guess...

See my post re the context

22 hours ago, MarkE said:

However, at least in my context, it is not really feasible to hack the selector that is returned from the Selector field. I have no idea what the selector might be.

(BTW, the context, in case it is relevant, is that I am iterating the selector test down the page tree, starting at the parent page's children and stopping at the first level that finds a match).

Now that I know the cause, my solution is to build a selector that always operates on the DB, by merging the user's selector with the one that was used to generate the Page Array:

	/**
	 * Find the first generation of children that matches the given selector.
	 *
	 * This method traverses the children of the given parent page, generation by generation,
	 * and returns the matches in first generation that has matches for the provided selector.
	 *
	 * @param Page $parentPage The parent page whose children are to be searched.
	 * @param string $selector The selector to match against the children.
	 * @return PageArray Returns a PageArray of the first generation that has matches, or an empty PageArray if no matches are found.
	 */
	public function findFirstMatchingGeneration(Page $parentPage, string $selector) {
		$currentGeneration = $parentPage->children();
		$count = 1;
		while($currentGeneration->count()) {
			$currentSelector = $currentGeneration->getSelectors(true);
			$matching = $this->pages()->find("$currentSelector, $selector");
			if($matching->count()) {
				return $matching; // Return the first generation that has matches
			}
			// Move to the next generation
			$nextGeneration = new PageArray();
			foreach($currentGeneration as $child) {
				$nextGeneration->add($child->children());
			}
			$count += 1;
			$currentGeneration = $nextGeneration;
		}
		return new PageArray(); // Return an empty PageArray if no matches are found
	}

 

  • Like 1
  • MarkE changed the title to 2-stage selector not working as expected [SOLVED]

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...