<p>Instead of trying to loop through an object (your Query) you could just execute it</p>

<pre><code>{% set component = {
    headline: headline ?? entry.headline ?? null,
    body: body ?? entry.body ?? null,
    items: items ?? entry.items.all() ?? null
} %}

&lt;h1&gt;{{ component.headline }}&lt;/h1&gt;
&lt;p&gt;{{ component.body }}&lt;/p&gt;

{% for item in component.items %}
    {{ item.name }}
{% endfor %}
</code></pre>

<p>or if that doesn't fit your case, you can do it before</p>

<pre><code>{% set relatedEntry = entry.callToAction.one() %}
{% include '_components/callToAction' with {
    entry: relatedEntry,
    items: relatedEntry.items.all()
} %}
</code></pre>

<p>You could as well check if your variable is an object of type <code>EntryQuery</code></p>

<pre><code>{% if className(entry) == 'craft\elements\db\EntryQuery' %}
    {% set entry = entry.one() %}
{% endif %}
</code></pre>

<p>Note: since you had multiple of these deprecated errors I wasn't exactly sure which one you wanted to "solve" and which one is "intended"</p>

CC BY-SA 4.0
2018-06-06T12:03:15.187Z
2018-06-06T12:10:54.390Z
2018-06-06T12:10:54.390Z
Robin Schambach
<p>I have a component that accepts both hardcoded objects and entry queries. This allows us to have components that can be used independent of the database.</p>

<p><strong>Hardcode include:</strong></p>

<pre><code>{% include '_components/callToAction' with {
    headline: 'Hardcode call to CTA Headline',
    body: 'Hardcode call to CTA',
    items: [
      { name: 'item1'},
      { name: 'item2'} 
    ]
} %}
</code></pre>

<p><strong>db include:</strong></p>

<pre><code>{% include '_components/callToAction' with {
    entry: entry.callToAction
} %}
</code></pre>

<p><strong>Component:</strong></p>

<pre><code>{% set component = {
  headline: headline ?? entry.headline ?? null,
  body: body ?? entry.body ?? null,
  items: items ?? entry.items ?? null
} %}

&lt;h1&gt;{{ component.headline }}&lt;/h1&gt;
&lt;p&gt;{{ component.body }}&lt;/p&gt;

{% for item in component.items %}
  {{ item.name }}
{% endfor %}
</code></pre>

<p>Both those work fine, but with Craft 3 we're getting deprecation errors for not using <code>.all()</code> when it is an element query which is drastically increasing our db queries on the pages that use several of these components.</p>

CC BY-SA 4.0
2018-06-05T14:04:09.427Z
And why do you refuse to execute the query? Seems no difference to me. Of course it makes sense in certain scenarios, but not in yours
Robin Schambach
CC BY-SA 4.0
2018-06-05T14:52:02.090Z
Not sure I understand, but `items` in the component won't work on the hardcoded include with .all() but it throws an error without it when it is run with the db include.
Jeremy A
CC BY-SA 4.0
2018-06-05T15:23:55.407Z
You can either check if the current instance of  `items` is an element of type ElementQuery or you pass the related items within it. `entry: entry.callToAction, items: entry.items.all()` or `items: items ?? entry.items.all() ?? null` I just want to understand why you don't want to execute it
Robin Schambach
CC BY-SA 4.0
2018-06-06T11:50:05.677Z
@RobinSchambach - I do want to execute it, I updated my example code which I think is where the confusion is coming from.
Jeremy A
<craft3>
For loop that can accept array and element query without throwing .all deprecation error
# For loop that can accept array and element query without throwing .all deprecation error

(Asked by: username_0 on 2018-06-05)

I have a component that accepts both hardcoded objects and entry queries. This allows us to have components that can be used independent of the database.
Hardcode include:
{% include '_components/callToAction' with {
    headline: 'Hardcode call to CTA Headline',
    body: 'Hardcode call to CTA',
    items: [
      { name: 'item1'},
      { name: 'item2'} 
    ]
} %}

db include:
{% include '_components/callToAction' with {
    entry: entry.callToAction
} %}

Component:
{% set component = {
  headline: headline ?? entry.headline ?? null,
  body: body ?? entry.body ?? null,
  items: items ?? entry.items ?? null
} %}

{{ component.headline }}
{{ component.body }}

{% for item in component.items %}
  {{ item.name }}
{% endfor %}

Both those work fine, but with Craft 3 we're getting deprecation errors for not using .all() when it is an element query which is drastically increasing our db queries on the pages that use several of these components.

**Tags:** craft3

> **Comments:**
> **username_1**: And why do you refuse to execute the query? Seems no difference to me. Of course it makes sense in certain scenarios, but not in yours  
> **username_0**: Not sure I understand, but `items` in the component won't work on the hardcoded include with .all() but it throws an error without it when it is run with the db include.  
> **username_1**: You can either check if the current instance of  `items` is an element of type ElementQuery or you pass the related items within it. `entry: entry.callToAction, items: entry.items.all()` or `items: items ?? entry.items.all() ?? null` I just want to understand why you don't want to execute it  
> **username_0**: @RobinSchambach - I do want to execute it, I updated my example code which I think is where the confusion is coming from.  

---

## 1 answers

### Answer by username_1 on 2018-06-06. Score: 1

Instead of trying to loop through an object (your Query) you could just execute it
{% set component = {
    headline: headline ?? entry.headline ?? null,
    body: body ?? entry.body ?? null,
    items: items ?? entry.items.all() ?? null
} %}

{{ component.headline }}
{{ component.body }}

{% for item in component.items %}
    {{ item.name }}
{% endfor %}

or if that doesn't fit your case, you can do it before
{% set relatedEntry = entry.callToAction.one() %}
{% include '_components/callToAction' with {
    entry: relatedEntry,
    items: relatedEntry.items.all()
} %}

You could as well check if your variable is an object of type EntryQuery
{% if className(entry) == 'craft\elements\db\EntryQuery' %}
    {% set entry = entry.one() %}
{% endif %}

Note: since you had multiple of these deprecated errors I wasn't exactly sure which one you wanted to "solve" and which one is "intended"


---

