Skip to content

[5.x]: PHP error when eager loading an element owner that is not the same element type as the nested element #16339

@lukeholder

Description

@lukeholder

What happened?

Description

The following code will cause the error:

{% set addresses = craft.addresses.with(['owner']).all() %}

This is due to

'elementType' => get_class(reset($sourceElements)),

expecting that the element type for the eager loading of the parent is the same element type as the source element.

Adding a matrix field to a category element and using this query can also cause the error:

{% set matrix = craft.entries.with(['owner']).all() %}

Since the category is not an entry element type.

Steps to reproduce

Add this code to a front-end template:

{% set addresses = craft.addresses.with(['owner']).all() %}

Actual behavior

PHP error

Related issue:

craftcms/commerce#3817

Fixed manually in Commerce with:

craftcms/commerce@5eacab1

Craft CMS version

5.x

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions