New in Craft CMS 2.6
Craft CMS 2.6 and Craft Commerce 1.1 were released yesterday. The most important new feature is eager loading for elements, an improvement that justifies the release on its own.
This article is over 10 years old. Features and approaches described here may have changed since.
Eager loading
Anyone who has worked with nested element queries in Craft knows the problem. Every time entry.featuredImage is called inside a loop, Craft fires a separate database query. With 20 articles each having an image, that's 20 extra queries. With nested relationships, for example articles with an author and avatar, those database hits multiply quickly into hundreds per page request.
The new with parameter preloads all related elements in a single query:
{% set entries = craft.entries.section('articles').with('featuredImage').find() %}
20 articles with images now cost one extra query instead of twenty. With complex data models, that's the difference between a fast page and a slow one.
The following elements support eager loading:
- Relations via entries, files, users, tags, and categories
- Matrix blocks
- Image transform indexes
withTransform can also be used to preload image transforms, rather than calculating them individually on each getUrl() call. Third-party plugins can add eager loading support via the new IEagerLoadingFieldType interface.
More additions
- Private asset sources – Asset sources can now be marked as private by disabling the "Assets in this source have public URLs" setting. This makes it possible for the first time to manage files behind access control, for example downloads only available to registered users.
- Rich Text fields – Each field can now specify which asset sources and image transforms are available to editors in the Rich Text editor.
- Client edition – Admins can now define which permissions the client account has. Previously these were fixed.
- {% case "foo" or "bar" %} – Multiple values can now be combined in a single case within a switch tag.
- Default entry status – Channels and structures can define a default status for new entries, useful for editorial workflows with approval processes.
- New dashboard widget – "New Users" shows an overview of recently created user accounts.
My take
Craft 2.6 has one feature that justifies the release on its own: eager loading. Where hundreds of database queries per page request were possible before, there are now just a handful. For developers, this was the moment Craft sites could stay performant even with complex data models, no workarounds, no plugins, just .with() added to the query. The rest of the release fills in the picture, but eager loading is why 2.6 is worth remembering.