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
Posted (edited)

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

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.