<p>What about creating an array of IDs with only those entries that do have an Thumbs image assigned to it and then check in your loop if you have to query the image at all:</p>

<pre><code>{% set entries = craft.entries.section('myChannel') %}

{# Get all potential "thumbs" using one! of the following ElementCriteriaModels #}
{% set thumbs = craft.assets.sourceId(1) %}
{% set thumbs = craft.assets.relatedTo(entries) %}

{# Get the IDs of those entries that are related to a "thumbs" image #}
{% set thumbEntryIds = craft.entries.section('myChannel').relatedTo({
    targetElement: thumbs,
    field: 'myImagesField',
}).ids() %}

{# Loop entries #}
{% for entry in entries %}

    {# Only request image if relationship exists #}
    {% if entry.id in thumbEntryIds %}
        &lt;img src="{{ entry.myImagesField.first().url }}"/&gt;
    {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-10-28T10:43:00.100Z
This isn't a bad idea. It feels "dirty" but it'll do the job, I suppose. Have you tested to see how many actual extra queries it'll add? Only 3 extra.. right?
naboovalley
CC BY-SA 3.0
2014-10-28T10:47:44.857Z
That's what I thought, but no, I didn't check how many queries there are and to be honest, I didn't test the code at all, @naboo.
carlcs
CC BY-SA 3.0
2014-10-28T11:02:18.897Z
One thing I'm totally not sure about, is how expensive that `relatedTo` parameter is to get the IDs of entries with thumbs. Maybe someone who knows more about what happens behind the code can help us to find that out?
carlcs
CC BY-SA 3.0
2014-10-28T12:16:12.623Z
When you run the `relatedTo` parameter on a regular Model it's not very "heavy" at all (in query execution time).
naboovalley
CC BY-SA 3.0
2014-10-28T09:55:18.027Z
2014-10-28T09:55:18.027Z
carlcs
<p>There isn’t currently, but it’s on our list to make it possible.</p>

<p><strong>UPDATE</strong></p>

<p>As of Craft 2.5, you can set relational field params to <code>'not :empty:'</code> or <code>':notempty:'</code> to only show elements where <em>something</em> is selected within the field; and you can set them to <code>':empty:'</code> to only show elements where <em>nothing</em> is selected within the field.</p>

CC BY-SA 3.0
2014-10-28T10:01:35.297Z
Brandon, that's not fair! "The List" answers will always be the most best-liked ones...
carlcs
CC BY-SA 3.0
2014-10-28T14:59:14.150Z
@carlcs Just start answering everything with "It's on The List!"
Brad Bell
CC BY-SA 3.0
2014-10-28T09:30:14.770Z
2015-12-05T15:18:59.727Z
2015-12-05T15:18:59.727Z
Brandon Kelly
<p>Is there any way to check if a relationship field has a value/is assigned to other elements (assets/entries) without having to add an extra query?</p>

<p>I have a Channel with an Asset field assigned to it. Now let's say I have a list of 100 entries. This list should show a thumbnail and the entry title. I only want to "fetch" the related image thumbnail if the user has choosen to select any image to reduce the amount of queries used.</p>

<p>The "standard" way which results in ~100 queries</p>

<pre><code>{% set image = entry.image.first() %}
{% if image %}
    &lt;img src="{{ image.url }}"/&gt;
{% endif %}
</code></pre>

<p>The way I tried it, but <em>|length</em> adds (1) query making it ~100 queries + the queries for each found thumbnail</p>

<pre><code>{% if entry.image|length &gt; 0 %}
    {% set image = entry.image.first() %}
    &lt;img src="{{ image.url }}"/&gt;
{% endif %}
</code></pre>

<p>Is there a third option I can use to check (without a query) is the <em>entry.image</em> has any related content assigned to it?</p>

<p>Thank you</p>

CC BY-SA 3.0
2014-10-27T16:06:44.097Z
Didn't you ask that same question last week already? :D
carlcs
CC BY-SA 3.0
2014-10-27T16:38:32.657Z
possible duplicate of [Optimize queries with list of image thumbnails](http://craftcms.stackexchange.com/questions/4166/optimize-queries-with-list-of-image-thumbnails)
Brad Bell
CC BY-SA 3.0
2014-10-27T20:36:39.080Z
Hi @bradbell. Here I'm asking if there's any option to check if a relationship field has any relations without running a query. This to avoid running a query to check if (for example) an Asset field has any assets attached to it. My other thread was a general question about if there was any better practice for long list of entries with thumbnails. This is -slightly- different. :)
naboovalley
CC BY-SA 3.0
2014-10-27T20:37:14.343Z
@carlcs naa this is -slightly- different ;)
naboovalley
CC BY-SA 3.0
2014-10-27T20:39:41.670Z
To clarify (so I'm not missunderstod). I'm asking if there's any way to see if a relationship field has any relations directly on the field without running a query (like if there's any default value added to the *entry.field* data). I just used the "Asset thumbnail example" again.
naboovalley
CC BY-SA 3.0
2014-10-28T09:57:40.470Z
Ahh alright, I now see that little difference. I just undid my close vote ;)
carlcs
<templating>
Check if a Relationship field has a relationship without extra query
# Check if a Relationship field has a relationship without extra query

(Asked by: username_0 on 2014-10-27)

Is there any way to check if a relationship field has a value/is assigned to other elements (assets/entries) without having to add an extra query?
I have a Channel with an Asset field assigned to it. Now let's say I have a list of 100 entries. This list should show a thumbnail and the entry title. I only want to "fetch" the related image thumbnail if the user has choosen to select any image to reduce the amount of queries used.
The "standard" way which results in ~100 queries
{% set image = entry.image.first() %}
{% if image %}
    
{% endif %}

The way I tried it, but |length adds (1) query making it ~100 queries + the queries for each found thumbnail
{% if entry.image|length > 0 %}
    {% set image = entry.image.first() %}
    
{% endif %}

Is there a third option I can use to check (without a query) is the entry.image has any related content assigned to it?
Thank you

**Tags:** templating
> **Comments:**
> **username_1**: Didn't you ask that same question last week already? :D  
> **username_2**: possible duplicate of [Optimize queries with list of image thumbnails](http://craftcms.stackexchange.com/questions/4166/optimize-queries-with-list-of-image-thumbnails)  
> **username_0**: Hi @bradbell. Here I'm asking if there's any option to check if a relationship field has any relations without running a query. This to avoid running a query to check if (for example) an Asset field has any assets attached to it. My other thread was a general question about if there was any better practice for long list of entries with thumbnails. This is -slightly- different. :)  
> **username_0**: @carlcs naa this is -slightly- different ;)  
> **username_0**: To clarify (so I'm not missunderstod). I'm asking if there's any way to see if a relationship field has any relations directly on the field without running a query (like if there's any default value added to the *entry.field* data). I just used the "Asset thumbnail example" again.  
> **username_1**: Ahh alright, I now see that little difference. I just undid my close vote ;)  

---

## 2 answers

### Answer by username_3 on 2014-10-28. Score: 7 | Accepted answer

There isn’t currently, but it’s on our list to make it possible.

**UPDATE**

As of Craft 2.5, you can set relational field params to `'not :empty:'` or `':notempty:'` to only show elements where *something* is selected within the field; and you can set them to `':empty:'` to only show elements where *nothing* is selected within the field.

> **Comments:**
> **username_1**: Brandon, that's not fair! "The List" answers will always be the most best-liked ones...  
> **username_2**: @carlcs Just start answering everything with "It's on The List!"  

---

### Answer by username_1 on 2014-10-28. Score: 1

What about creating an array of IDs with only those entries that do have an Thumbs image assigned to it and then check in your loop if you have to query the image at all:
{% set entries = craft.entries.section('myChannel') %}

{# Get all potential "thumbs" using one! of the following ElementCriteriaModels #}
{% set thumbs = craft.assets.sourceId(1) %}
{% set thumbs = craft.assets.relatedTo(entries) %}

{# Get the IDs of those entries that are related to a "thumbs" image #}
{% set thumbEntryIds = craft.entries.section('myChannel').relatedTo({
    targetElement: thumbs,
    field: 'myImagesField',
}).ids() %}

{# Loop entries #}
{% for entry in entries %}

    {# Only request image if relationship exists #}
    {% if entry.id in thumbEntryIds %}
        
    {% endif %}

{% endfor %}

> **Comments:**
> **username_0**: This isn't a bad idea. It feels "dirty" but it'll do the job, I suppose. Have you tested to see how many actual extra queries it'll add? Only 3 extra.. right?  
> **username_1**: That's what I thought, but no, I didn't check how many queries there are and to be honest, I didn't test the code at all, @naboo.  
> **username_1**: One thing I'm totally not sure about, is how expensive that `relatedTo` parameter is to get the IDs of entries with thumbs. Maybe someone who knows more about what happens behind the code can help us to find that out?  
> **username_0**: When you run the `relatedTo` parameter on a regular Model it's not very "heavy" at all (in query execution time).  

---

