1

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 %}
    <img src="{{ image.url }}"/>
{% 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() %}
    <img src="{{ image.url }}"/>
{% 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

  • Didn't you ask that same question last week already? :D – carlcs Oct 27 '14 at 16:06
  • possible duplicate of Optimize queries with list of image thumbnails – Brad Bell Oct 27 '14 at 16:38
  • 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 Oct 27 '14 at 20:36
  • @carlcs naa this is -slightly- different ;) – naboovalley Oct 27 '14 at 20:37
  • 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 Oct 27 '14 at 20:39
6

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.

  • Brandon, that's not fair! "The List" answers will always be the most best-liked ones... – carlcs Oct 28 '14 at 10:01
  • 1
    @carlcs Just start answering everything with "It's on The List!" – Brad Bell Oct 28 '14 at 14:59
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 %}
        <img src="{{ entry.myImagesField.first().url }}"/>
    {% endif %}

{% endfor %}
  • 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 Oct 28 '14 at 10:43
  • 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 Oct 28 '14 at 10:47
  • 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 Oct 28 '14 at 11:02
  • When you run the relatedTo parameter on a regular Model it's not very "heavy" at all (in query execution time). – naboovalley Oct 28 '14 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.