7
votes
Accepted
Eager Loading a Structure and descendants
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
votes
Accepted
Eager Loading Users Returning "Array to String Conversion" Error
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 ...
4
votes
Understanding Eager Loading
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 ...
4
votes
Accepted
How to improve performance of this nested entries/array template?
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-...
4
votes
How to improve performance of this nested entries/array template?
Have you considered to cache the whole output? https://craftcms.com/docs/templating/cache
4
votes
Ordering entries based on an eager-loaded element's custom field
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 ...
4
votes
Accepted
Eager Load Matrix Field with Assets Field Type
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 ...
4
votes
Using Focuspoint with eager loading
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 (...
4
votes
Accepted
Eager-Loading Assets in Matrix
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 ...
4
votes
Accepted
Craft incredibly slow site performance
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...
4
votes
Accepted
How to speed up a single entry page with Eager Loading
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 ...
4
votes
Accepted
Is there a way to eager-load fields in a global set?
I know you can use this method to eager load fields on injected entry/category variables, so I'm assuming it would work for global sets as well:
{% do craft.app.elements.eagerLoadElements(
...
3
votes
Accepted
Craft 3: query matrixBlocks and eager load its owner?
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/...
3
votes
Accepted
Enforce search match multiple parameters
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 ...
3
votes
Understanding Eager Loading
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 ...
3
votes
Accepted
Getting an array of ids from an array of eager-loaded elements within Twig
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 ...
3
votes
Accepted
Eager-load and pagination
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 ...
3
votes
Accepted
Eager-Loading child elements in a multi-level structure section
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: ['...
3
votes
Accepted
Eager load Imager transforms in Craft Commerce
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
votes
Accepted
Eager loading fields within categories
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 ...
3
votes
Accepted
Correctly using eager loading in Plugin development
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 ...
3
votes
Accepted
Eager loading on entry not reducing queries
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.
...
3
votes
Accepted
Eager loading category data from a Matrix block
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-...
3
votes
Accepted
Eager loading of variant images
You can eager-load elements that were already loaded (like an Entry or Product that Craft set up for you "outside" a template) like this:
{% do craft.app.elements.eagerLoadElements(
...
2
votes
Accepted
Eager loading EntryElementType from custom ElementType in a plugin
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 ...
2
votes
Getting an array of ids from an array of eager-loaded elements within Twig
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 = ...
2
votes
Can I eager load Super Table fields in a Matrix block?
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
votes
Accepted
Eager loading with conditions
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 ...
2
votes
Accepted
Using Focuspoint with eager loading
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 ...
2
votes
Accepted
Starting & Ending Loops with Eager Loaded Elements
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 %}...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
eager-loading × 63performance × 12
elementcriteriamodel × 8
templating × 7
matrix × 7
entries × 6
assets × 5
plugin-development × 4
plugin-craftcommerce × 4
categories × 4
relations × 3
image × 3
query × 3
craft3 × 2
users × 2
search × 2
structure × 2
pagination × 2
array × 2
globals × 2
entrymodel × 2
queries × 2
plugin-imager × 2
element × 2
plugin-focuspoint × 2