Jump to content

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


thetuningspoon
 Share

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
Link to comment
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
Link to comment
Share on other sites

  • 2 months later...

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
Link to comment
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
Link to comment
Share on other sites

  • 1 year later...
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. 

Link to comment
Share on other sites

  • 1 year later...
On 5/18/2018 at 10:45 PM, Robin S said:

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)

 

Maybe things have changed in the last couple years but I can't find anything to do with joining in the official docs. 

Is this api still available for deciding what fields are auto joined? 

I'd love to have one query that gives me all the data available without needing to do round robin SQL queries. 

Link to comment
Share on other sites

5 hours ago, StanLindsey said:

Is this api still available for deciding what fields are auto joined?

Yes, it's still the same as it was, it's just that in the documentation the getById() method has been replaced with getByIDs() since v3.0.156. See "joinFields" here: https://processwire.com/api/ref/pages/get-by-i-ds/

Example with $pages->find():

$items = $pages->find("template=colour", ['loadOptions' => ['joinFields' => ['headline', 'text_1']]]);

 

  • Like 1
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...