1

I'm looping through channel entries. When I try to output the value of a relationship field, I seem to be getting some sort of numerical id (it's not the entry id of the current channel or the channel it is related to; I checked). What exactly is that number? How is this number related to the channel it relates to?

Ultimately, I'm trying to change the structure of the data for some other use later on. So, is there anything in the two channels that are identical so that I can connect the two?

UPDATE 1: code snippet

<ul>
    {exp:channel:entries channel="regions" sort="asc" orderby="title" disable="categories|member_data|pagination"}
        <li>{title} - {entry_id}</li>
    {/exp:channel:entries}
</ul>

<?php $formatted_branches = array(); ?>
{exp:channel:entries channel="regions" sort="asc" orderby="title" disable="categories|member_data|pagination"}
    {reverse_related_entries sort="asc" orderby="title" channel="branch"}
        <?php
            $formatted_branches["{participant_status}"]["{region}"]["{parent_company}"][] = array('branch_name' => "{branch_name}");
        ?>
    {/reverse_related_entries}
{/exp:channel:entries}
<?php var_dump($formatted_branches); ?>
2
  • 1
    Can you update your question with the template code?
    – Siebird
    Dec 4, 2012 at 23:49
  • Updated. The code works. It's just that {region} seems a bit useless. I need to be able to relate it to list items in the ul above. Dec 5, 2012 at 0:19

1 Answer 1

2

The value is a rel_id from the exp_relationships table, which is used as a linking table for all relationships.

Typically you want to use the Relationship tags provided by the Channel Entries loop to work with relationship fields, you should not generally attempt to use them as single variables since they are not generally useful for anything (expect in some special circumstances such as working with custom plugin code).

It looks like you are trying to reference a related entry from inside of a {reverse_related_entries} loop - the same issue applies here, the Relationship field itself only returns the rel_id value - you need to use Channel Entries tag and {related_entries} to turn this into a usable value.

You could do this by using an embed, for instance, which would load the given entry from the {reverse_related_entries} loop, then loop over it's {related_entries} loop:

Main template

{exp:channel:entries channel="regions" sort="asc" orderby="title" disable="categories|member_data|pagination"}
    {reverse_related_entries sort="asc" orderby="title" channel="branch"}
        {embed="pages/branch_block" entry_id="{entry_id}"}
    {/reverse_related_entries}
{/exp:channel:entries}

pages/branch_block embed:

{exp:channel:entries entry_id="{embed:entry_id}" dynamic="off" disable="categories|member_data|pagination"}
    {related_entries sort="asc" orderby="title" channel="branch"}
        {entry_id} - {title}<br/>
    {/related_entries}
{/exp:channel:entries}

However, note that using embeds in this way is typically not recommended since it causes nested database calls. If you have a limited number of entries in this relationship however, it's probably okay.

You may also want to consider using Playa instead of the built in Relationships field. It's template tags may be closer to what you expect.

I should also point out that your use of PHP in this template is probably something you want to avoid. It would be a lot better to do this work in a plugin, then package up the results and return it to the template. PHP should never be turned on in templates. I gave a talk on creating simple plugins and wrote up a blog post about it that you may find interesting.

6
  • Thanks, but I'm not clear on your suggestion. I'm actually within {reverse_related_entries} tag pairs. In it, I'm looping through a channel that has a relationship field. What can I do inside that loop to get something useful from the relationship field (an entry id would be useful, perhaps). Dec 5, 2012 at 0:14
  • If I understand your usage correctly, {region} is itself the related entry. You need to use the {related_entry} loop to de-reference the relationship value to an actual entry. Dec 5, 2012 at 0:50
  • You are right, {region} is a relationship field. I've new to EE and have never used this {related_entry} tag. Will look it up. Dec 5, 2012 at 0:57
  • I've updated my answer with more details based on your sample code. Dec 5, 2012 at 0:58
  • I'm looking into this. I'm stuck using PHP for now as I am new to EE and still need to learn how to make plugins. Must I use an embed or can I get away without it? Dec 5, 2012 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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