ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Trouble with a relationship field and the relationship entries tag

November 02, 2012 3:11pm

Subscribe [3]
  • #1 / Nov 02, 2012 3:11pm

    GabbaTheHutt's avatar

    GabbaTheHutt

    84 posts

    Hi peeps

    I’m having real trouble with a relationship field and the relationship entries tag.

    1. The relation field seems to be causing trouble with the function of a channel entries tag. When a value, which exists in the relationship field, is placed in the segment_3 position, the channel entries tag stop showing any data.
    2. The data stored in the relationship field is displaying as a 3-digit number and not as the text that has been entered.
    3. When I put a relationship entries tag into the channel entries tag, the relationship field won’t display, even if it the relationship entries tag is commented out.

    The result of this is that I cannot use segment_3 to pass the client_id into the URL and I can’t then use the client_id from the URL to filter the channel entries tag, because it is the wrong value.

    I am guessing that you will need to login to the site to see what is going on, so I won’t add any code snippets or anything.

    Many thanks
    Gabe

  • #2 / Nov 05, 2012 4:32pm

    Dan Decker

    7338 posts

    Hi Gabe,

    Can we see your template code?

    I suspect part of the problem can be solved with dynamic=“no”, but it would be best if we could see the overall context with how you are using related entries.

    Cheers,

  • #3 / Nov 06, 2012 5:01pm

    GabbaTheHutt's avatar

    GabbaTheHutt

    84 posts

    That seems to have fixed items 2 & 3, but item one still exists. The template code is as follows:

    {doctype}
    {html_begin}
    {head_begin}
    {stylesheets}
    {head_end}
    <body id="{segment_1}-{segment_2}">
    {wrapper_begin}
    {header_begin}
    <h1>FPC Sugarcraft</h1>
    {header_end}
    {main_begin}
    <h3>Client Details</h3>
    
    <p><table></p>
    
    <p><tr><br />
    <th>Client ID</th><br />
    <th>Client Name</th><br />
    <th>Client in Sage</th><br />
    </tr></p>
    
    <p>{exp:channel:entries channel="client_system" search:client_id="{segment_3}" order_by="title" sort="asc"}</p>
    
    <p><tr><br />
    <td class="client-id">{title}</td><br />
    <td class="client-name"><a href="http://{site_url}index.php/pages/client_details/{client_id}">{client_name}</a></td><br />
    <td class="client-in-sage">{client_in_sage}</td><br />
    </tr></p>
    
    <p>{/exp:channel:entries}</p>
    
    <p></table><br />
    <br></p>
    
    <h3>Orders</h3>
    
    <p><table><br />
    <tr><br />
    <th>Order ID</th><br />
    <th>Order Date</th><br />
    <th>Client ID</th><br />
    <th>Client Name</th><br />
    <th>Packaging Type</th><br />
    <th>Destination Country</th><br />
    <th>Delivery Cost</th><br />
    <th>Campaign ID</th><br />
    </tr></p>
    
    <p>{exp:channel:entries channel="order_system" search:order_client_id="{segment_3}" orderby="title" sort="asc" dynamic="no"}</p>
    
    <p><tr><br />
    <td>{title}</td><br />
    <td>{order_date}</td><br />
    <td>{order_client_id}</td><br />
    <td>{related_entries id="order_client_id"}{client_name}{/related_entries}</td><br />
    <td>{order_packaging_type}</td><br />
    <td>{order_destination_country}</td><br />
    <td>{order_delivery_cost}</td><br />
    <td>{order_campaign_id}</td><br />
    </tr></p>
    
    <p>{/exp:channel:entries}</p>
    
    <p></table></p>
    
    <p>{main_end}<br />
    {footer_begin}<br />
    Footer<br />
    {footer_end}<br />
    {wrapper_end}<br />
    {body_end}<br />
    {html_end}

  • #4 / Nov 07, 2012 3:40pm

    Dan Decker

    7338 posts

    Hey Gabe,

    I’m not certain that relationship fields are supported in the Search parameter. Per the User Guide:

    Note: Only fields of the type “Text Input”, “Textarea”, and “Drop-down Lists” are searched with this parameter.

    I betting that’s the reason things aren’t quite right for you.

    Can you describe a bit about what you are trying to do and I may be able to suggest a different approach?

    Cheers,

  • #5 / Nov 08, 2012 1:59pm

    GabbaTheHutt's avatar

    GabbaTheHutt

    84 posts

    OK when I create a text field the search works.

    What I am trying to do is to link channels together, using a unique id, so that I don’t have to duplicate information. In this case the Client ID is manually entered into the Order System, using the Order Client ID field and I am then using a Related Entries tag to pull in the Client Name from the Client System. The Order Client ID field is a Relationship Field in order to link the two channels using the client ID. I am searching by the Order Client ID in order that I can provide a list of orders for that client.

    Is there a better way of doing this?

    Could you also explain why it is that Relationship Fields are not searchable, as I don’t understand this?

    Many thanks
    Gabe

  • #6 / Nov 09, 2012 5:02pm

    Robin Sowell's avatar

    Robin Sowell

    13218 posts

    When a relationship is created, EE gathers up all of the data on the child and puts it into a single field in the exp_relationships table.  It’s a form of caching- if it had to recreate all of that data on the fly, it would seriously bog things down.

    So your client ID and such is all in a single field in a separate table- and the exp_channel_data table holds only a single ID in the custom field that ties back to that relationship data. 

    That’s why if you look at the raw data in a relationship field, it’s a single number.  It’s the relationship_id= and matches the id stored in the relationships table.

    The search parameter only ‘searches’ the data table, which is why relationships aren’t allowed as an option there.  And searching the relationship table is problematic because the data is condensed into a single field.

    So….  How to work around this issue.

    Instead of relationships, can you flip out the channel and use reverse relationships?

    {exp:channel:entries channel="clients" search:order_client_id="{segment_3}" orderby="title" sort="asc" dynamic="no"}
    
    {reverse_related_entries orderby="title"}
    <tr>
    <td>{title}</td>
    <td>{order_date}</td>
    <td>{order_client_id}</td>
    {/reverse_related_entries}
    <td>{client_name}</td>
    {reverse_related_entries orderby="title"}
    <td>{order_packaging_type}</td>
    <td>{order_destination_country}</td>
    <td>{order_delivery_cost}</td>
    <td>{order_campaign_id}</td>
    </tr>
    {/reverse_related_entries}
    {/exp:channel:entries}

    It depends on exactly what you want it to do- but I think this may be an option. 

    The other alternative I can think of would be to use some extensions and automatically save the client ID in the order channel in a field of its own.  This would take some programming, but wouldn’t be hard for someone w/a bit of PHP knowledge.  It could be done manually as well- but that’s really clunky.

    Think one of the above is a viable option?

  • #7 / Nov 10, 2012 2:20pm

    GabbaTheHutt's avatar

    GabbaTheHutt

    84 posts

    Hi Robin

    Thanks for the information. It is really helpful. A lot of that information would be really useful in the User Guide as I have not seen it in there.

    This would seem to be a good solution. The problem seems to be that I am trying to search by the Client ID (or Order Client ID) but I need to search by the relationship_id, but I don’t know what this is… How can I work out what the relationship_id for a particular client, so that I can post it in the url, from the link to this page?

    Cheers
    Gabe

  • #8 / Nov 13, 2012 7:21pm

    Robin Sowell's avatar

    Robin Sowell

    13218 posts

    Yep, relationships are complicated- the wiki has a good article that goes into even more detail if you think that will help.

    That said?  There isn’t a good way to directly search the data in the relationship table.  And the search parameter will not work on a relationship field.  Like I say- you COULD search on the client channel- and then show the products as reverse relationship.  That might work, depending a bit on the specifics.

    Otherwise?  I’d probably make a custom fieldd type thats hidden- and dump the client id in it directly.  This would take some custom coding to work out.  You’d want to be sure that field stays synced with the data in the client channel.

  • #9 / Nov 20, 2012 7:24am

    GabbaTheHutt's avatar

    GabbaTheHutt

    84 posts

    Hi Robin

    Right well that kind of works. It certainly provides the required data. The only thing is the way that it loops. Since the reverse_related_entries tag loops within itself, when the is more than one line of data, it is presented as extra cells to the right, in the same row, rather then starting a new row. Also, because there are two reverse_related_entries loops in the exp:channel:entries tag, they are both looping and creating some weird results. See the Orders section on the following page: http://workflow.fpcsugarcraft.com/pages/client_details_reverse/

    Any thoughts?

    Cheers
    Gabe

  • #10 / Nov 20, 2012 7:26am

    GabbaTheHutt's avatar

    GabbaTheHutt

    84 posts

    {doctype}
    {html_begin}
    {head_begin}
    {stylesheets}
    {head_end}
    <body id="{segment_1}-{segment_2}">
    {wrapper_begin}
    {header_begin}
    <h1>FPC Sugarcraft</h1>
    {header_end}
    {main_begin}
    <h3>Client Details</h3>
    
    <p><table></p>
    
    <p><tr><br />
    <th>Client ID</th><br />
    <th>Client Name</th><br />
    <th>Client in Sage</th><br />
    </tr></p>
    
    <p>{exp:channel:entries channel="client_system" search:client_id="{segment_3}" order_by="title" sort="asc"}</p>
    
    <p><tr><br />
    <td class="client-id">{title}</td><br />
    <td class="client-name"><a href="http://{site_url}index.php/pages/client_details/{client_id}">{client_name}</a></td><br />
    <td class="client-in-sage">{client_in_sage}</td><br />
    </tr></p>
    
    <p>{/exp:channel:entries}</p>
    
    <p></table><br />
    <br></p>
    
    <h3>Orders</h3>
    
    <p><table><br />
    <tr><br />
    <th>Order ID</th><br />
    <th>Order Date</th><br />
    <th>Client ID</th><br />
    <th>Client Name</th><br />
    <th>Packaging Type</th><br />
    <th>Destination Country</th><br />
    <th>Delivery Cost</th><br />
    <th>Campaign ID</th><br />
    </tr></p>
    
    <p>{exp:channel:entries channel="client_system" search:client_id="{segment_3}" orderby="title" sort="asc" dynamic="no"}</p>
    
    <p>{reverse_related_entries orderby="title"}<br />
    <tr><br />
    <td>{title}</td><br />
    <td>{order_date}</td><br />
    {/reverse_related_entries}<br />
    <td>{client_id}</td><br />
    <td>{client_name}</td><br />
    {reverse_related_entries orderby="title"}<br />
    <td>{order_packaging_type}</td><br />
    <td>{order_destination_country}</td><br />
    <td>{order_delivery_cost}</td><br />
    <td>{order_campaign_id}</td><br />
    </tr><br />
    {/reverse_related_entries}</p>
    
    <p>{/exp:channel:entries} </p>
    
    <p></table></p>
    
    <p>{main_end}<br />
    {footer_begin}<br />
    Footer<br />
    {footer_end}<br />
    {wrapper_end}<br />
    {body_end}<br />
    {html_end}

  • #11 / Nov 21, 2012 5:32pm

    Dan Decker

    7338 posts

    Hi Gabe,

    I think the problem is when there is no reverse related entry to display, it wrecks your table!

    You can account for this by using {if no_reverse_related_entries}:

    {reverse_related_entries orderby="title"}
    {if no_reverse_related_entries}
    <tr>
    <td> </td>
    <td> </td>
    {/if}
    <tr>
    <td>{title}</td>
    <td>{order_date}</td>
    {/reverse_related_entries}
    <td>{client_id}</td>
    <td>{client_name}</td>
    {reverse_related_entries orderby="title"}
    {if no_reverse_related_entries}
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    {/if}
    <td>{order_packaging_type}</td>
    <td>{order_destination_country}</td>
    <td>{order_delivery_cost}</td>
    <td>{order_campaign_id}</td>
    </tr>
    {/reverse_related_entries}
  • #12 / Nov 24, 2012 10:00am

    GabbaTheHutt's avatar

    GabbaTheHutt

    84 posts

    Hi Dan

    Thanks for your response. That would seem to make sense, but when I put that code in place, it makes no difference, sadly.

    For now I have decided to leave out the client_id and client_name, so this is no longer a high priority, but it is a little frustrating.

    Never mind!

    Cheers
    Gabe

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases