<p>The error you’re getting is a bug in Craft. (Just fixed that for the next release.)</p>

<p>To work around it, replace the <code>{% set authors ... %}</code> tag with:</p>



<pre><code>{% if bookIds|length %}
    {% set authors = craft.entries()
        .section('authors')
        .relatedTo({ sourceElement: bookIds, field: 'books' })
        .all() %}
{% else %}
    {% set authors = [] %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2017-10-17T15:36:52.437Z
Thanks very much Brandon, but it appears to be giving the same error: http://textuploader.com/d4uwl
Rivu Meyano
CC BY-SA 3.0
2017-10-17T15:38:27.857Z
@RivuMeyano What does `{{ dump(bookIds) }}` output for you?
Brandon Kelly
CC BY-SA 3.0
2017-10-17T16:01:24.247Z
If I only include that in my template, then I get and "Unknown "dump" function" error, but I guess that's because bookIds hasn't been defined.

It appears to be the first block of code that gives the  "Undefined offset: 0" error, but if I include that in order to have the Dump function do something, I will of course still get the  "Undefined offset: 0" error.
Rivu Meyano
CC BY-SA 3.0
2017-10-17T16:02:58.747Z
Sorry, you should enable Dev Mode first. (Set `'devMode' => true` in craft/config/general.php.) And you should put that tag after the `{% set bookIds = ...  %}` tag, but remove the author code after it.
Brandon Kelly
CC BY-SA 3.0
2017-10-17T16:06:42.173Z
Ah - here's what Dump gives me: https://pictr.com/image/UwhxP
Rivu Meyano
CC BY-SA 3.0
2017-10-17T16:08:19.333Z
Looks like it hasn’t even made it to the `dump()` function. It’s saying your `publisher` variable doesn’t exist. That was just a placeholder in the example for whatever your publisher entry variable is called. If you’re on the publisher’s entry template, maybe it’s actually `entry`?
Brandon Kelly
CC BY-SA 3.0
2017-10-17T16:15:26.170Z
Yes I'm on the publisher's entry template. The channel is Publisher, and the relationship field from Books to Publisher is also called Publisher.

If change the targetElement to 'entry' as in the link below, the dump function gives:  array(1) { [0]=> string(2) "33" }

http://textuploader.com/d4ubu
Rivu Meyano
CC BY-SA 3.0
2017-10-17T16:28:18.613Z
OK, and now if you remove the `dump()` function and bring back the author code, does it work as expected?
Brandon Kelly
CC BY-SA 3.0
2017-10-17T16:43:12.587Z
Yes the error's now gone, so that must be working (just have to figure out how to put the results on the page..). Thank you!
Rivu Meyano
CC BY-SA 3.0
2017-10-17T14:58:20.327Z
2017-10-17T14:58:20.327Z
Brandon Kelly
<p>I want to do exactly this: <strong><a href="https://github.com/craftcms/cms/issues/1640" rel="nofollow noreferrer">Multi-step &amp; bi-directional relations with relatedTo (#1640)</a></strong></p>

<blockquote>
  <p>Common example: Let’s say you have Books, Authors, and Publishers, and two relationship fields on the Books channel: <code>author</code> and <code>publisher</code>. On a Publisher’s page, you want to list all of the Authors that have published books with them.</p>
</blockquote>

<p>Here’s what I’ve tried:</p>

<pre><code>{% set bookIds = craft.entries()
.section('books')
.relatedTo({ targetElement: publisher, field: 'publisher' })
.ids() %}

{% set authors = craft.entries()
.section('authors')
.relatedTo({ sourceElement: bookIds, field: 'books' })
.all() %}
</code></pre>

<p>However the code results in an "Undefined offset: 0" error. The error seems to occur in the first block of code.</p>

<p>Additionally, I'm not sure about the field in the penultimate line, shouldn't it be 'authors' rather than 'books'?</p>

CC BY-SA 3.0
2017-10-17T10:42:53.337Z
Search your logs (`craft/storage/runtime/logs/craft.log`, etc.) for the error message you’re getting. Can you post the stack trace that gets logged with it?
Brandon Kelly
CC BY-SA 3.0
2017-10-17T10:46:51.243Z
Yes you’re right, `field: 'books'` should have been `field: 'author'`. Just updated the GH issue.
Brandon Kelly
CC BY-SA 3.0
2017-10-17T12:30:33.830Z
Thank you Brandon, here's the log:

http://textuploader.com/d4uuk
Rivu Meyano
<relations>
Filtering by cousins
# Filtering by cousins

(Asked by: username_0 on 2017-10-17)

I want to do exactly this: Multi-step & bi-directional relations with relatedTo (#1640)

Common example: Let’s say you have Books, Authors, and Publishers, and two relationship fields on the Books channel: author and publisher. On a Publisher’s page, you want to list all of the Authors that have published books with them.

Here’s what I’ve tried:
{% set bookIds = craft.entries()
.section('books')
.relatedTo({ targetElement: publisher, field: 'publisher' })
.ids() %}

{% set authors = craft.entries()
.section('authors')
.relatedTo({ sourceElement: bookIds, field: 'books' })
.all() %}

However the code results in an "Undefined offset: 0" error. The error seems to occur in the first block of code.
Additionally, I'm not sure about the field in the penultimate line, shouldn't it be 'authors' rather than 'books'?

**Tags:** relations
> **Comments:**
> **username_1**: Search your logs (`craft/storage/runtime/logs/craft.log`, etc.) for the error message you’re getting. Can you post the stack trace that gets logged with it?  
> **username_1**: Yes you’re right, `field: 'books'` should have been `field: 'author'`. Just updated the GH issue.  
> **username_0**: Thank you Brandon, here's the log:  http://textuploader.com/d4uuk  

---

## 1 answers

### Answer by username_1 on 2017-10-17. Score: 1 | Accepted answer

The error you’re getting is a bug in Craft. (Just fixed that for the next release.)
To work around it, replace the {% set authors ... %} tag with:
{% if bookIds|length %}
    {% set authors = craft.entries()
        .section('authors')
        .relatedTo({ sourceElement: bookIds, field: 'books' })
        .all() %}
{% else %}
    {% set authors = [] %}
{% endif %}

> **Comments:**
> **username_0**: Thanks very much Brandon, but it appears to be giving the same error: http://textuploader.com/d4uwl  
> **username_1**: @RivuMeyano What does `{{ dump(bookIds) }}` output for you?  
> **username_0**: If I only include that in my template, then I get and "Unknown "dump" function" error, but I guess that's because bookIds hasn't been defined.  It appears to be the first block of code that gives the  "Undefined offset: 0" error, but if I include that in order to have the Dump function do something, I will of course still get the  "Undefined offset: 0" error.  
> **username_1**: Sorry, you should enable Dev Mode first. (Set `'devMode' => true` in craft/config/general.php.) And you should put that tag after the `{% set bookIds = ...  %}` tag, but remove the author code after it.  
> **username_0**: Ah - here's what Dump gives me: https://pictr.com/image/UwhxP  
> **username_1**: Looks like it hasn’t even made it to the `dump()` function. It’s saying your `publisher` variable doesn’t exist. That was just a placeholder in the example for whatever your publisher entry variable is called. If you’re on the publisher’s entry template, maybe it’s actually `entry`?  
> **username_0**: Yes I'm on the publisher's entry template. The channel is Publisher, and the relationship field from Books to Publisher is also called Publisher.  If change the targetElement to 'entry' as in the link below, the dump function gives:  array(1) { [0]=> string(2) "33" }  http://textuploader.com/d4ubu  
> **username_1**: OK, and now if you remove the `dump()` function and bring back the author code, does it work as expected?  
> **username_0**: Yes the error's now gone, so that must be working (just have to figure out how to put the results on the page..). Thank you!  

---

