craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.27k stars 635 forks source link

[4.x]: Elements when eager loaded are null #12251

Closed martinherweg closed 1 year ago

martinherweg commented 1 year ago

What happened?

Description

I have a query like this

{% set newsQuery = craft.entries.section('news').with(['heroImage', 'topics', 'category']).limit(4).all() %}
{% for news in newsQuery %}
  {{ d(news.topics.one()) }}
{{ d(news.category.one() }}
{% endfor %}

The result of the dump is null for all elements, the topics are Entries and are surely set on these elements and the category for example works fine when eager loaded. If I remove topics from the .with() I get results as wished.

I also have two different environments and it works on one and not on the other.

Expected behavior

news.topics.one() should return the element when eager loaded

Actual behavior

Craft CMS version

4.3.1

PHP version

8.0.25

Operating system and version

Linux 5.10.0-0.bpo.15-amd64

Database type and version

MySQL 10.3.36

Image driver and version

No response

Installed plugins and versions

brianjhanson commented 1 year ago

Just for a sanity check, do you get the same output when using Craft's native dump function or dd tag?

Assuming the output is the same, do you have any custom code that might be interfering with eager loading? Could you also specify the field type of each of the eager loaded elements (heroImage, topics, and category)?

martinherweg commented 1 year ago

@brianjhanson Yes same for dump or dd tag

I test this on a new template without any block nor extending anything so it's solely the query because that was my first assumption as well that there is something getting in the way.

heroImage is an assets field category is an Entries field topics is an Entries field

I want to say again that it works fine in another envrionment, so it might be something with the database? The company I'm working at also has a support plan so I could open a case there and send you a database there.

brianjhanson commented 1 year ago

That'd be great. If it works somewhere it definitely sounds like an environment issue.

martinherweg commented 1 year ago

👍 Thanks brian I'll do that tomorrow.

brianjhanson commented 1 year ago

Sounds like the one and only @olivierbon helped you get things back on track.

For other's wandering into this thread, the issue ended up being two fields with the same handle. The fix was to remove one of the fields and manually fix things up.