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 · 2 comments

Comments

Projects
None yet
3 participants
@d13creative
Copy link

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

This comment has been minimized.

Copy link

commented Jan 16, 2019

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

This comment has been minimized.

Copy link
Member

commented Jan 16, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.