navigation icon indicating copy to clipboard operation
navigation copied to clipboard

Eager Loading not working

Open plumduffer opened this issue 2 years ago • 3 comments

Describe the bug

I am trying to use eager loading to cut down on duplicate queries and I have noticed that eager loading doesn't seem to be doing anything with this plugin.

For example, here is one of my lines:

{% set primaryNavigation = craft.navigation.nodes('primaryNavigation').level(1).with(['children.children']).all() %}

While everything is functional, I noticed there is no difference in the amount of queries whether the with function is there or not. Additionally, when die and dumping like this {% dd primaryNavigation[0].children %}, it displays that the type is of verbb\navigation\elements\db\NodeQuery#1. Should this not be an array?

Is anyone else currently having this issue?

Steps to reproduce

  1. Try and eager load child nodes.
  2. Notice it doesn't work.

Craft CMS version

4.3.7.1

Plugin version

2.0.16

Multi-site?

Yes

Additional context

No response

plumduffer avatar Feb 08 '23 02:02 plumduffer

My workaround for the time being:

{% set primaryNavigation = craft.navigation.nodes('primaryNavigation').all() %}

{% for navItem in primaryNavigation|filter(x => x.level == 1) %}
    {% set childrenNav = primaryNavigation|filter(x => x.level > 1 and x.parent.id == navItem.id) %}
{% endfor %}

And so on.

plumduffer avatar Feb 09 '23 20:02 plumduffer

Having this issue as well. On a local dev site with only 3 nodes and one subnode:

Without eager loading (children called using .all()):

image

With eager loading as per docs:

image

Removing the Navigation query entirely, leaving everything else:

image

Interestingly enough, ripping everything out of the header file except for this one line:

{% set primaryNodes = craft.navigation.nodes('headerNavigation').level(1).with(['children']).all() %}

Still leaves us at 100ms:

image

That's odd.

@plumduffer updated my local to 4.3.8.2 and it seems to be fixed, likely related to https://github.com/craftcms/cms/issues/12648:

image

mightyfineyall avatar Feb 10 '23 17:02 mightyfineyall

There have been some rough issues with eager loading in Craft itself, so be sure to get updated before proceeding further.

However, the lack of eager-loading nodes is a well-known issue. While the nodes themselves can be eager-loaded, the linked-to elements cannot and is at the moment a Craft limitation.

engram-design avatar Feb 11 '23 03:02 engram-design