super-table
super-table copied to clipboard
Add eager loading examples to docs
Hey there. In #66 you said you'd added eager loading to SuperTable. Are you able to give an example of how to go about this?
Would you be able to update with Wiki (or docs) with a few examples. i.e. maybe:
Based on your example above, I would have though something along the lines of:
{% set tableRows = entry.superTableDemoField.find({ with: [ "imageField" ] }) %}
And how would you access that in twig?
What about the case where we have a matrix field, with a block that allows a supertable row to be used for an image and an alt text (see below).
How would you access that? Something like:
{% set bannerSlides = entry.featuredBanner.find({ with: [ 'bannerImage', # Or 'bannerImage.image', # Or 'bannerImage:image', # Or ] }) %}
And how would you access that in Twig?
I really could use some examples too.
+1 for this.
@JamesMcFall Apologies for the delayed response to this, but based on your first example, that would be correct. Accessing it is very similar to Matrix:
{% set blocks = craft.superTableField.find({
with: ['assetField'],
}) %}
{% for block in entry.superTableField %}
{% set image = block.assetField[0] ?? null %}
{% if image %}
<img src="{{ image.url }}" alt="{{ image.title }}">
{% endif %}
{% endfor %}
or
{% set entries = craft.entries({
section: 'eagerLoading',
with: ['superTableField.assetField'],
}) %}
{% for entry in entries %}
{% for block in entry.superTableField %}
{% set image = block.assetField[0] ?? null %}
{% if image %}
<img src="{{ image.url }}" alt="{{ image.title }}">
{% endif %}
{% endfor %}
{% endfor %}
But, onto the bad news. You can't currently eager load Matrix and Super Table combinations. This is firstly, because if you have a Matrix > Super Table combination, there's no way to tell the native Matrix how to handle eager loading of the Super Table field. We'd need to write our own function to eager load this combination. Secondly, even if you did it the other way (Super Table > Matrix), we need some modifiers to determine if things are a Matrix field, Matrix block, or regular field.
So at the moment, its (annoyingly) just simple eager loading support.
Hey @engram-design.
Thanks for getting back to me.
It'd be rad if that example could be put into the docs as others might find it useful.
I totally get the matrix/supertable combo issues. I ran into that problem yesterday actually - and it pained me to not be able to solve it, but I guess that's what caching is for :P
Thanks again!
Hey @engram-design, just to be clear, is it technically impossible to add support for eager loading Super Table rows within a Matrix field, or is it just very challenging? If it's the latter, is it something you plan on tackling in the future? Thank you very much for your great work!
@benface I've honestly not looked into it, so I'm not sure if its possible or not. Its pretty low on the priority list though, as just about every new development for Super Table has been put on hold (indefinitely) with the news of nested Matrix coming to Craft 4. My priority is mostly around supporting the existing functionality.
Then again, just haven't looked into this that deeply, so I'll let everyone know if its worth the effort.
Thanks, appreciate the response and your great work @engram-design!
Hey @engram-design, sorry to resurrect an old topic, but there still isn't eager loading info on the docs. Additionally, how would we go about eager loading from a Vizy field?
E.g. In my Vizy template example.twig I have a ST called 'images' and this has a asset field called 'logo', how do we perform one SQL query rather than n+1?
If I use the above example the eager loaded response goes from being an AssetQuery to null.
I haven't had the chance to do this (in fact, really waiting for Craft 5 to supersede Super Table altogether), but I will mention that when combined with Vizy you cannot eager-load though a Vizy field. Here's a similar comment for Hyper, which uses a similar mechanism for storing content that isn't an element.
So you can eager-load fields from the Vizy field, but you can't eager-load the Entry > Vizy > Super Table field, because Vizy isn't an element.