1

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'?

Brandon Kelly
  • 34,307
  • 2
  • 71
  • 137

1 Answers1

1

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 %}
Brandon Kelly
  • 34,307
  • 2
  • 71
  • 137
  • Thanks very much Brandon, but it appears to be giving the same error: http://textuploader.com/d4uwl – Rivu Meyano Oct 17 '17 at 15:36
  • @RivuMeyano What does {{ dump(bookIds) }} output for you? – Brandon Kelly Oct 17 '17 at 15:38
  • 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 Oct 17 '17 at 16:01
  • 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 Oct 17 '17 at 16:02
  • Ah - here's what Dump gives me: https://pictr.com/image/UwhxP – Rivu Meyano Oct 17 '17 at 16:06
  • 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 Oct 17 '17 at 16:08
  • 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 Oct 17 '17 at 16:15
  • OK, and now if you remove the dump() function and bring back the author code, does it work as expected? – Brandon Kelly Oct 17 '17 at 16:28
  • 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 Oct 17 '17 at 16:43