Jump to content
Remi

Problem with find method (sort and more)

Recommended Posts

Hi!

I've got a page with text area where user can check some codes divided by space, semicolon or comma.

I can use two solutions to search for these codes:

First method:

$prCodesArray = preg_split('/[\\s,;]+/', $prCodes, -1);
$selector = "title=" . implode("|", $prCodesArray) . ", template=pr-code, include=all, limit=100";
$prCodePages = $pages->find($selector);
$prCodeCount = count($prCodePages);
if ($prCodeCount) {
	foreach ($prCodePages as $prCodePage) {
		$content .= $prCodePage->title . " - " . "<span title=\"". $prCodePage->parent->pr_code_group_description . "\">" . $prCodePage->parent->title . "</span> - " . $prCodePage->pr_code_description . "<br>";
	}
} else {
	$content .= $prCode . __(' - Not found.') . "<br>";
}

Advantages:
+ it's fast ~0.21s for ~40 codes.

Disadvantages:
- alphabetical results sorting while I want input order,
- doesn't show not found codes.

 

Second method:

$prCodesArray = preg_split('/[\\s,;]+/', $prCodes, -1);
foreach ($prCodesArray as $prCode){
	$selector = "title={$prCode}, template=pr-code, include=all, limit=10";
	$prCodePages = $pages->find($selector);
	$prCodeCount = count($prCodePages);
	if ($prCodeCount) {
		foreach ($prCodePages as $prCodePage) {
			$content .= $prCodePage->title . " - " . "<span title=\"". $prCodePage->parent->pr_code_group_description . "\">" . $prCodePage->parent->title . "</span> - " . $prCodePage->pr_code_description . "<br>";
		}
		else {
			$content .= $prCode . __(' - Not found.') . "<br>";
		}
	}
}

Advantages:
+ sorting like input order,
+ shows not found codes.

Disadvantages:
- it's slow as hell ~2.5s for ~40 codes.

 

Is there any solution to join advantages of both methods?

 

Also, as you can see I have to use include=all statement in my find methods and I don't know why. I've used Import Pages from CSV module. All codes (pages) are published and there is no access control on any of them.

Share this post


Link to post
Share on other sites

In your first version you can use ->find() on $prCodePages again to filter by single code in memory. This way you retrieve codes in one sweep from the db and filter them later to get results by single code.

  • Like 2

Share this post


Link to post
Share on other sites
51 minutes ago, LostKobrakai said:

In your first version you can use ->find() on $prCodePages again to filter by single code in memory. This way you retrieve codes in one sweep from the db and filter them later to get results by single code.

I'll try that :)

It works!

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By opalepatrick
      Hi, trying to order pages by modified date, date. I can do 'sort=-date_modified, sort=-date' but that will always put the modified date first regardless of actual date order. I just want to coalesce the two fields and sort. Any ideas would be appreciated.
    • By Noel Boss
      Hi everyone 🙌
      Is there a way to order the language tabs in the backend? Currently, they seem to be ordered based on the created date (or id?) …
      ( time passes … ⏰ 🚶‍♂️)
      Just answered my own question. One can reorder the languages in the page tree under admin » languages or directly in the DB » pages » template_id=54 » sord field.
      Anyone knows if there are side effects when moving the default language from sort 0 to somewhere higher?
    • By schwarzdesign
      ProcessWire added the numReferences property in 3.0.107, which returns the count of all pages having a reference to the page object. However, apparently it's not possible to use this property inside a selector. I'm trying to find all pages that are referenced at least once:
      $pages->find('template=service, numReferences>0'); This throws an error: "Field does not exist: numReferences".
      Is there another way to filter by the number of references? Of course, I could manually filter the results of the find query, but that feels overly complicated. Ideally, I would also like to filter the number of references from a specific field; that is, find all pages that are referenced at least once in one specific page reference field.
      Is there a way to do this? I guess supporting numReferences in selectors would be a feature request - if so, is it feasible?
      Thanks!
       
    • By pwFoo
      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()?
×
×
  • Create New...