Jump to content
thetuningspoon

Autojoining fields with a specific $pages->find()

Recommended Posts

Not a question, but just something I discovered that I wanted to share on the forum since I haven't seen it discussed anywhere. If you are familiar with the "autojoin" option on individual ProcessWire fields, enabling this feature means that every time a page with that field on it is loaded into memory, the field will be included with it immediately. (Normally just the page's name and meta data is included, and getting a specific field from the page requires a separate trip to the database. This is to conserve memory)

Normally this is fine and it keeps things simple, but occasionally you may be loading a lot of data in a consistent fashion (for, example, to populate a table) and know exactly which fields you need. With the autojoin option on fields, you would have to enable autojoin for all pages at all times, which you probably don't want.

With the following code, you can do a $pages->find() and specify exactly which fields you want to autojoin with it:

$pages->find("template=whatever", ['loadOptions' => ['joinFields' => ['filed1','field2','field3']]] );

I tested out in debug mode on one of my projects, and it seemed to significantly reduce the number of SQL queries required for an html data table I was building.

Hope that helps someone!

  • Like 11
  • Thanks 1

Share this post


Link to post
Share on other sites

Nice tip!

2 hours ago, thetuningspoon said:

With the autojoin option on fields, you would have to enable autojoin for all pages at all times, which you probably don't want.

You can also turn autojoin on and off for selected fields whenever you need via API methods $field->addFlag(Field::flagAutojoin) and $field->removeFlag(Field::flagAutojoin).

 

  • Like 4

Share this post


Link to post
Share on other sites

I think it would be easier to find that great tip if the topic moved to the Tutorials section.

Edited by cstevensjr
Moderator's Note: Moved to Tutorials for wider availability of this information

Share this post


Link to post
Share on other sites
3 hours ago, dragan said:

Maybe @ryan could include that info in the official docs?

I think of the API reference as the official docs rather than the cheatsheet. The cheatsheet hasn't had a comprehensive update for a long time and there are loads of things in the API reference that aren't in the cheatsheet.

The loadOptions / joinFields options are covered in the API reference:

https://processwire.com/api/ref/pages/find/
loadOptions (array): Optional associative array of options to pass to getById() load options.

https://processwire.com/api/ref/pages/get-by-id/
joinFields (array): Autojoin the field names specified in this array, regardless of field settings (requires autojoin=true). (default=empty)

 

  • Like 1

Share this post


Link to post
Share on other sites
On 2/21/2018 at 3:16 PM, Robin S said:

Nice tip!

You can also turn autojoin on and off for selected fields whenever you need via API methods $field->addFlag(Field::flagAutojoin) and $field->removeFlag(Field::flagAutojoin).

 

Something else important to note here:  If you are returning pages with your find that have page fields on them, and you are subsequently accessing fields from those subpages that you also want to have autojoined, you should use Robin's method rather than passing in the joinFields option into your find.

It's worth noting that autojoining a page reference field only automatically joins its id. It still requires a separate database query to get the page, which occurs the first time you try to access that field or one of its subfields.

If you have the fields you want to be autojoined flagged, they will be loaded along with the page at that time. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...