7

Craft 2.6.2789 added this feature, which is seems you've reverse engineered. :) It is now possible to eager-load Structure entries' and categories' descendants using the 'descendants' handle.


5

Just checked eager-loading a text field in a template and by eager-loading it you are actually converting the field to an array and therefore unable to template it as usual. Eager-loading is generally only useful on fields like categories, assets, and matrixes.


4

Your syntax is a bit off. The withTransforms parameter is an Asset criteria, meaning it is only relevant when you're pulling Assets. Appending it to entry.relatedArticles (an Entries field, returning an ElementCriteriaModel) like you're currently attempting does nothing, but this does: {% set assets = entry.someAssetsField.find({ withTransforms: ['...


4

Caching will improve performance, but the actual issue is that your code is generating a lot of queries. You should look into eager loading: https://craftcms.com/docs/templating/eager-loading-elements The basic idea is that you predefine what sub-elements you're going to need (in your case gig_artist and gig_venue), so that Craft can preload them instead ...


4

Have you considered to cache the whole output? https://craftcms.com/docs/templating/cache


4

This requires a bit of extra work on the ElementQuery, but it can be done in the same query. We need to perform an inner join on the relation between the movie and the show and then another inner join on the show's content table. Finally we can order on the custom field of the show's content table. {% set currentMovies = craft.entries() .section('...


4

The line {% set img = entry.primaryImage[0] ?? null %} is looking for the primaryImage field on the entry inside your for loop. It is not looking for primaryImage for the current matrix field. If primaryImage is a field within your matrix blog you will need to change the line to {% set img = block.primaryImage[0] ?? null %}


4

After some research I found out that this problem has no simple solution (unfortunately). Check https://github.com/smcyr/Craft-FocusPoint/issues/10. A solution would be not to use eager loading (obvious). Another solution could be using Craft3 (no plugin needed). The solution I used was Focal Point (https://github.com/aelvan/FocalPointField-Craft). Focal ...


4

The reason this doesn't work is because .type('image') won't work with eager loading. Eager loading removes the ability to use ElementCriteriaModel methods. From the docs: When the assets aren’t eager-loaded, entry.assetsField gives you an ElementCriteriaModel that is preconfigured to return the related assets once they’re requested (e.g. when first() is ...


4

It turned out this was the culprit. Craft was doing a deprecationerrors query (https://github.com/craftcms/cms/issues/2539). Turning devMode to false fixed this. Hope this helps someone...


3

Currently no, but I suppose since Craft 3 added the craft.matrixBlocks() template function, it would make sense to make this possible. Just created a FR about this: https://github.com/craftcms/cms/issues/1820


3

I think you're misunderstanding what with does. Or I am. Ha! From what I understand with is for eager loading, but doesn't control anything in the relation itself. I don't think your relation makes any sense either because all you'll do is substitute a string into your spec variables. Try something more like: {% set products = craft.commerce.products({ ...


3

Thanks for this Mats. Is there a general guide as to when you should really use Eager loading? I am not seeing much improvement The article Speed up your Craft CMS Templates with Eager Loading I wrote hopefully gives you the "general guide" you asked for.


3

You would just loop the entry models to collect the IDs (looks ugly, but it's a simple and efficient for loop). But in case you do not need anything else from the models, I'd actually expect your previous approach using the ids method to have better performance. {% set ids = [] %} {% for entry in entry.myEagerLoadedEntries %} {% set ids = ids|merge([...


3

with is just another query param, like limit or whatever parameters you’re defining in entryParams. The {% paginate %} tag requires that you pass in an ElementCriteriaModel, but as soon as you call find() you’re actually dealing with an array of the matched entries. So the key to getting eager-loading working with pagination is to assign the with param ...


3

To use eager-loading for the child elements add 'children' to the with parameter. {% set mice = craft.entries({ section: 'experiments', level: 3, descendantOf: entry.id, with: ['children', {order: 'postDate asc'}], }) %}


3

Eager loading is used to reduce the number of database queries. Imager doesn't store any data in the database, it's completely file-based, so it's not relevant to use eager-loading in relation to it.


3

This is basically just a syntax error. What you are doing is: searching for entries in the group section: 'markets', with: ['bannerImage'] but what you want is searching for categories in the group markets with the field bannerImage {% set markets = craft.categories.group('markets').with(['bannerImage']).all() %} The group parameter in your CategoryQuery ...


3

Eager loading only works when your field is a RelationField it does not work with Text fields. If you goal is to reduce the length of the queries and to maximize your performance you could fetch the raw rows instead of populating the elements. $criteria = craft()->elements->getCriteria(ElementType::Category); $criteria->status = null; $criteria->...


3

Disclaimer: While eager-loading will almost always bring down the total number of queries executed, it's important to note that having less queries won't always equal better performance. When optimising a page with eager-loading, you should take care to observe the actual time Craft spends on database queries – which is the metric that matters. For Craft 3, ...


3

I don't think eager loading will help in this case because you're trying to eager load fields with only a single relationship (asset). It's not the n+1 query problem eager loading is meant to solve. If you had an asset field with multiple assets selected, you would see some benefit. But even then, if there are only a few assets, the small penalty of eager ...


3

Apologies, I misread the question the first time around. When eager loading via a matrix block, you need to provide a blocktype as well as the field name. See: https://docs.craftcms.com/v3/dev/eager-loading-elements.html#eager-loading-elements-related-to-matrix-blocks for more details


2

The article on StraightUpCraft should be able to point you in the right direction. PHP stuff is towards the bottom of the article. Examples of Eager Loading Elements in Twig and PHP Also this Craft hangout is worth watching, it gives a lot of detail on Eager loading. It's over an hour long though, so set yourself some time aside to watch it. Eager loading ...


2

You could use the indexBy parameter and the keys filter: {# Using indexBy updates the results array to be indexed by the Element ID instead of a consecutively numbered array #} {% set entries = craft.entries.section('news').with(['featuredImage']).indexBy('id') %} {# The keys filter returns an array of all the keys within an array, discarding the ...


2

This appears to have been added in the 0.4.7 release! View the original issue here. Note: I have not personally verified this functionality.


2

Yeah, but you need to make sure you pass an array to the with param and you can’t compose an array by injecting a string into the [ ] like you did in your example. Try something like this. {% set withParam = [] %} {% if displayType == 'teaser' %} {% set withParam = ['leadImage', 'type', 'indexHeading', 'brief'] %} {% endif %} {% if someCondition %} ...


2

I ran into this issue too and had landed on this thread (and another similar one). I was able to piece together a solution using the modify elements query method. I wrote a pull request that got merged, so lazy-loading works with this plugin as of version 1.1.4.


2

Found this nugget about Iterating over a Subset in the Twig docs about slice: {% for client in clients|slice(0, 8) %} ... {% endfor %} {% for client in clients|slice(8) %} ... {% endfor %} More details about slice.


2

First, based on your example code I don't think you actually need to loop on the entries – you could simply group them on the brand name, and then loop on the keys for the grouped array (the keys would be your brand names): {% set brands = allEntries|group('brand')|keys %} {% for brand in brands %} <option value="{{ brand }}">{{ brand }}</...


2

The original answer is good, but as noted, somewhat incorrect, at least concerning a chainable .with() method. Here's my attempt to explain things: Speed up your Craft CMS Templates with Eager Loading


Only top voted, non community-wiki answers of a minimum length are eligible