fix for primary owner not getting eager loaded and the fallback failing too#16960
Closed
fix for primary owner not getting eager loaded and the fallback failing too#16960
Conversation
Member
|
This solution isn’t working for me in a similar test:
Template: {% for entry in craft.entries.field('contactMethods') %}
<li>{{ entry.id }} - {{ entry.label }}, owner: {{ entry.getPrimaryOwner() }}</li>
{% endfor %}Output: ( But regardless, the eager-loading added by 8cb3e36 is still important, so I don’t think this is the right approach. Instead I’ve PR’d support for eager-loading elements of multiple element types at once (#16972). Combined with removing the @i-just can you confirm that you’re also seeing the expected output on your test with that PR? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
If you run a query that gets both nested and root entries, the primary owner for nested elements won’t get loaded if the first entry returned by the query:
This will then lead to the
getSupportedSites()returning wrong sites, and an attempt to save a nested element that was grabbed this way will fail with anAttempting to save an element in an unsupported siteerror.Steps to reproduce:
clean v5 install of 5.6.2 or above
create 2 sites (
siteAandsiteB)create entry type
article, all default settings, no fields needed except for the default titlecreate a section
blogthat uses thearticleentry typecreate matrix field (
matrix) that uses thearticleentry type; propagation method set tononecreate a global set (
myGlobalSet) that has thematrixfield in it’s field layoutcreate an entry in the
blogsectionedit
myGlobalSetand forsiteBonly add an entry to the matrix field; savecreate a template file with the following code:
access the template via browser and note that for the blog entry, the “supported sites” lists both
siteAandsiteBand for the entry from the matrix field from the global, the “supported sites” is listed correctly assiteBnow create a second entry (must be created after adding the entry to the global set) in the
blogsectionaccess the template again and note that for the first blog entry, the “supported sites” lists both
siteAandsiteB; for the entry from the matrix field from the global, the “supported sites” is listed wrongly as the primary site (siteA) and for the second blog entry, the “supported sites” also correctly lists both sitesThe problem:
The problem is that the
eagerLoadingMapin theNestedElementTraittakes theownerTypeof the first source element. If the first source element is a root entry, then there’s no owner, and therefore no owner type, so the map isfalse, and nothing gets eager loaded.If the first source element is nested but its owner is of a different type than the owners of other source elements (e.g., the first source element’s owner is a Category, but there are other source elements in the set that are owned by Global Set or Entry, etc.), then the eager loading map will only look through the Categories, and so other source element owners won’t be eager loaded as expected.
The fallback we have to get the primary owner if it wasn’t eager loaded will then fail because the
primaryOwnerIdis lost (set tonull) in the process of eager loading. If theprimaryOwnerwasn’t found during eager loading, it’ll still be set as eager loaded as anullowner which will remove theprimaryOwnerIdfrom the nested element we were getting it for which will cause the fallback for getting theprimaryOwnerto fail.(related to this change)
Solution:
When we reach the fallback for getting the nested element’s primary owner, and we can’t get the owner type, we should try to deduct it from the
primaryOwnerIdwe had at the start of the method and only bail if we can’t get it that way too.Related issues
n/a - from a query that @olivierbon was working on in support