Jump to content

Selector over all fields?


h4r4ld
 Share

Recommended Posts

Hi,

I need to get all pages from a template where a searchstring matches any field of the page.

$result = $pages->find('template=documentHolder, status=hidden, *=$string');

I know i could do something like

$result = $pages->find('template=documentHolder, status=hidden, (field1=$string), (field2=$string), (field3=$string)'); 

But is there a way to do so without writing down every field the template has? Maybe something like a subselector?

Best,

Harri

Link to comment
Share on other sites

You could just ask the template to give you all fields it does posses:

$selector = "..." . $template->fields->implode("|", "name") . "*=$string";

If you can specify all field beforehand you could use FieldtypeCache and add all necessary fields to it. After that you can just search the cache field instead of all other fields.

  • Like 3
Link to comment
Share on other sites

For what reason would you want to do that? It sounds terribly inefficient. You want every field for the matching template or every field? (edit: Completely misread your post, sorry)

You can also do field1|field2|field3=$string, however if you are absolutely set on every field I think you can do.

$f = $fields->find("template=documentHolder");
$results = $pages->find("template=documentHolder, status=hidden, $f=$string");

But I'm not absolutely sure, I'm just guessing here after using ProcessWire for a couple of months.

edit: LostKobrakai beat me to it :P His answer will be correct as he is way better than me at this ProcessWire stuff.

  • Like 1
Link to comment
Share on other sites

Thanks, exactly what I was looking for.

If you can specify all field beforehand you could use FieldtypeCache and add all necessary fields to it. After that you can just search the cache field instead of all other fields.

Can you provide some documentation on this? I didn't find anything related?

Link to comment
Share on other sites

I doubt there's any official documentation, but there's actually not much more to know about. It combines the string contents of each specified field when the page is saved and stores it encoded in json in the database. The same does happen when a page is loaded to populate the runtime accessable fielddata. If you then search the field and the keyword is somewhere in the json blob, than it's somewhere in your other fields, too. Besides that it's handled just like any other field as well. If you've any further questions just ask them here.

  • Like 1
Link to comment
Share on other sites

That would fit quiet well and is what I expected it to do, I just don't found information like:

Is this cachefield generated automatically or on demand?

If on demand, what do I have to do?

Can I select fields which should be in or is it all fields of a template/page?

Usage is exactly like a "normal" field in a selector?

Link to comment
Share on other sites

Just create one of those fields and most of those will be answered already by the descriptions/notes in the field editing. Essentially you don't have to worry about anything, create the field, add it to a template, save the page once or regenerate the cache by using the option in the field editing interface and add it like any other field to your selector. Now you can essentially forget it's even there.

  • Like 1
Link to comment
Share on other sites

Ok, so I made a cache field like

ojIyE3x.png

with autojoin on. If I'm right I should be able to do something like

$allDocuments = $pages->count("doccache%=".$input->post->request)

echo "There are " . $allDocuments . " docs containing your request"; 

Well, I don't get any results while using request that should match. I tried to add new document pages and regenerated the cache, but that ain't helped.

Where's my mistake?

Link to comment
Share on other sites

Ok, so I made a cache field like

(img)

with autojoin on. If I'm right I should be able to do something like

$allDocuments = $pages->count("doccache%=".$input->post->request)

echo "There are " . $allDocuments . " docs containing your request"; 
Well, I don't get any results while using request that should match. I tried to add new document pages and regenerated the cache, but that ain't helped.

Where's my mistake?

I believe you should be using

$allDocuments = $pages->find("doccache%={$input->post->request}");

echo "There are $allDocuments->count() documents containing your request."
Edited by LostKobrakai
stripped image from quote
Link to comment
Share on other sites

@Tom.

That's actually the same in outcome, just a magnitute worse from the performance standpoint. $pages->count() does let mysql do the counting, whereas your code does first load all found pages to memory and then count them with php.

Thank you very much for that Lost, my apologies. I didn't know you could do a selector inside a count. You learn something new every day :-)

Link to comment
Share on other sites

I retake my old topic, because it somehow fits from the title. :)

I want to add a dropdowninput field where the dropdown is filled by all given fieldvalues from one field of a template. I just need every value once, so I could do this with some php but I was wondering if PW has a function for that?

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