Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager loading broken on assets and entry fields within matrix blocks #3644

Closed
d13creative opened this issue Jan 16, 2019 · 6 comments
Closed

Comments

@d13creative
Copy link

d13creative commented Jan 16, 2019

Entry and asset fields within a matrix block return an empty array when eager loading after updating to 3.1

The following code generates the error "Key "0" does not exist as the array is empty". Prior to updating to 3.1 this was working ok:

(site is a Global Set, logos is a matrix field which has one 'logo' block type)

{% set logos = site.logos
    .with([
        ['logo:image', { withTransforms: [ 'logo', 'logo2x' ]} ]
    ])
    .all() %}

{% for logo in logos %}
{% set image = logo.image[0] %}
    {% if logo.website | length %}
        <li><a href="{{ logo.website }}"><img class="lozad" data-src="{{ image.getUrl('logo') }}" data-srcset="{{ image.getUrl('logo') }} 1x, {{ image.getUrl('logo2x') }} 2x" alt="{{ logo.logoTitle }}"></a></li>
        {% else %}
        <li><img class="lozad" data-src="{{ image.getUrl('logo') }}" data-srcset="{{ image.getUrl('logo') }} 1x, {{ image.getUrl('logo2x') }} 2x" alt="{{ logo.logoTitle }}"></li>
    {% endif %}
{% endfor %}

If I change the code to this the code works:

{% set logos = site.logos.all() %}
{% for logo in logos %}
{% set image = logo.image.one() %}
    {% if logo.website | length %}
        <li><a href="{{ logo.website }}"><img class="lozad" data-src="{{ image.getUrl('logo') }}" data-srcset="{{ image.getUrl('logo') }} 1x, {{ image.getUrl('logo2x') }} 2x" alt="{{ logo.logoTitle }}"></a></li>
        {% else %}
        <li><img class="lozad" data-src="{{ image.getUrl('logo') }}" data-srcset="{{ image.getUrl('logo') }} 1x, {{ image.getUrl('logo2x') }} 2x" alt="{{ logo.logoTitle }}"></li>
    {% endif %}
{% endfor %}
  • Craft version: 3.1
  • PHP version: 7.3.1
@richardfrankza
Copy link

We are experiencing exactly the same issue with eager loading an image asset in a matrix block. Instead of returning AssetQuery it just returns an empty array.

@brandonkelly
Copy link
Member

Fixed for the next release. You can patch your installs in the meantime by editing composer.json:

"require": {
    "craftcms/cms": "dev-develop#f738158f3f264e5cbd48656338f7661c151408d1 as 3.1.0",
    "...": "..."
}

And then run composer update.

@JshGrn
Copy link

JshGrn commented Jul 7, 2021

I still get this exact issue, has it been reintroduced?

@brandonkelly
Copy link
Member

@JshGrn Not to our knowledge… can you provide your Craft version, and exactly what you’re seeing?

@JshGrn
Copy link

JshGrn commented Jul 8, 2021

If I do a .with on a matrix field eager loading the asset field it returns an empty array. I was able to work around it by doing a asset query withTransforms and then relatedTo the matrix field.

@brandonkelly
Copy link
Member

@JshGrn There is a special syntax for eager-loading fields nested within Matrix fields – see Eager-Loading Elements Related to Matrix Blocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants