Jump to content

hooked properties and selectors problem


owzim
 Share

Recommended Posts

I found something I'd consider a bug.

Let's say we have this very useless hook:

$pages->addHookProperty("Page::foo", null, function($event) {
	$page = $event->object;
	if ($page->title === "About") {
		$event->return = "About foo";
	}
});

When I search for pages with a non empty foo property:

$pages->find("foo!=''");

I get an exception:

Error: Exception: Field does not exist: foo (in /wire/core/PageFinder.php line 494)

Which I consider very IN YOUR FACE to begin with, even if the hooked property foo would not exist. If none of the pages have a field called foo, then just get me ZERO pages. BUT since this property is created via hook, PW should not tell me the field does not exist.

However, If I use the same search with a filter it works as expected, I get a PageArray with one result (bc there is one page with the title About):

$pages->find("*")->filter("foo!=''");

Please don't tell me this is expected behavior :'(

Link to comment
Share on other sites

Nooooo that steals away some very convenient flexibility from me, I just thought I had.

But it makes sense, thanks for the insight.

What immediately comes to mind is that selecting pages with say a limit of 10 and a nice pagination would be messed up if the filter does not spit out any pages before the first 10 results.

Are there any good practices dealing with this kind of situations?

Link to comment
Share on other sites

Are there any good practices dealing with this kind of situations?

First I'd rather find out what you are trying to achieve here :) It would be terribly slow to filter results based on a block of PHP code instead of a field in the database. If you need filtering and pagination based on this "property", you should implement it, whatever it is that you are implementing, as a database-field (one way or another). If you just think about it for a moment, you would otherwise need to fetch every single page in the database and then execute the hook to evaluate the value of the property and then decide whether the page should be included in the result or not. I would say there isn't a good practise to do that. But I'm pretty sure that what you are trying to do could be implemented as a field. Just fill us in a bit more!

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