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

Checking if in list causes per-element queries #144

Open
thisisjamessmith opened this issue Oct 11, 2024 · 1 comment
Open

Checking if in list causes per-element queries #144

thisisjamessmith opened this issue Oct 11, 2024 · 1 comment
Labels

Comments

@thisisjamessmith
Copy link

Question

In your docs for checking if an item is in the list, you have this:

{% for entry in craft.entries.section('news').all() %}
    {% if list.getItem(entry) %}
        <a href="{{ list.removeItemUrl(entry) }}">Remove from List</a>
    {% else %}
        <a href="{{ list.addItemUrl(entry) }}">Add to List</a>
    {% endif %}
{% endfor %}

However list.getItem(entry) appears to cause a per-element DB query, resulting in many extra queries on long listing pages. I was going to look into solving this with some simple Twig arrays loaded upfront, but before I do that I wanted to check I wasn't missing something or making a mistake/omission in my implementation. Can we eager load this information in getUserList() or something?

Additional context

No response

@engram-design
Copy link
Member

So while we do eager-load things with list->getItems(), this function doesn't use that, purely because you can pass in additional query params as a second argument.

I'll give this some extra thought for when you supply additional params, as I might have to make that a breaking change to just filter the eager-loaded items (with your provided params) rather than element query params. But this is going to disrupt anyone doing non-equality checks in params.

But - for everyone else that just wants to check an element "normally", I think we certainly should be using the eager-loaded items, and filter just that.

Updated for the next release. To get this early, run composer require verbb/wishlist:"dev-craft-5 as 3.0.3".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants