0

I have a channel called 'models' containing a grid field called 'size_chart'. I have another channel called 'photos' with a grid field called 'photo_meta' inside which I have a relationship field called 'model' which is related to entries in the 'models' channel.

I want to be able to loop through the models and only show photos they are in, however I can't seem to get the right syntax with the relationship field buried away inside the the grid field type. This is as far as I have got..

{exp:channel:entries channel="models"}
  {title}
    {parents channel="photos" field="photo-meta:model"}
       <!-- Photo and title -->
      {parents:title}
      {parents:image}
      <!-- Model info -->
      {size_chart:height}
      {size_chart:waist}
    {/parents}
{/exp:channel:entries}
1
  • Did you ever resolve this, we have similar challenges?
    – paulcripps
    Feb 5, 2015 at 9:50

3 Answers 3

1

Just noticed a line at the bottom of the docs for Relationships which answers my own question:

"The Relationships field can be used as a Grid field column. Currently it is not possible to get the parents of a relationship field that is inside of a Grid field. You can also not use Relationships inside of a Grid field that does not store Channel data."

1

I made a quick simple plugin that works around this issue. The plugin returns a pipe-separated list of entry ids which can then be used in a channel entries tag's entry_id or fixed_order parameters.

https://github.com/thisisjamessmith/ee-grid-relationship-parents

I'd recommend then integrating that list of ids into your preferred Stash coding pattern, probably using match and against to filter against a larger list.

0

Bringing up a stale topic, you can totally achieve parent relationships within a grid field. You just need to use PHP to circumvent the Grid itself! The relationship data is 100% ok, it's just the parents tag doesn't resolve to the right place (a grid instead of an entry).

The syntax is actually quite straight-forward so I've included an example here.

What we're going to do is get the parents ourselves with PHP, and utilize the EE parse order to process an embed that will display the parents info.

For this example I've deliberately kept things simple, I've made 2 channels (parent_channel and child_channel), the parent_channel has a grid on it with a label(text) and child(relationship) column on it. I've populated my entries as so:

  • Even Children (parent_channel)
    • Child 2 (child_channel)
    • Child 4 (child_channel)
    • Child 6 (child_channel)
  • Odd Children (parent_channel)
    • Child 1 (child_channel)
    • Child 3 (child_channel)
    • Child 5 (child_channel)

So now, as we expect from the OP we are iterating some child entries, we have a pretty standard channel entries loop here:

PHP is ON, parsed on Output

{exp:channel:entries channel="child_channel"}

    <h2>{title}</h2>

    <strong><em>Parents</em></strong>
    <br />
    <?php
        $res = ee()->db->get_where('relationships', array('child_id' => '{entry_id}'));

        foreach($res->result_array() as $parent){
            print '{embed="templates/.parent" eid="'.$parent['parent_id'].'"}';
        }
    ?>
    <hr />
{/exp:channel:entries}

Here, please note the simple query that interrogates the relationship table. this gets out relevant parents. In this example each child only has one parent however if you note the foreach loop will take care of multiple parents. Also note how we print the embed tags with php, this way we can avoid any unforeseen 'PHP inside an embed tab, inside a channel entries tag on a template' hell, though it may not be required!

So now for that embedded template, here it really is as simple as it gets, no need for PHP but it's a standard template, as standard as it gets so you can use PHP, any EE tags, whatever you want my friends!

{exp:channel:entries dynamic="no" entry_id="{embed:eid}"}
    {title}
{/exp:channel:entries}

There you have it, winner winner, chicken dinner! To make my answer 110% complete, heres the output I get (please note I'm using EE2.8.1 in my sandbox, but this all should be correct for EE 2.9.X too).

Child 6

Parents
Even Children


Child 5

Parents
Odd Children


Child 4

Parents
Even Children


Child 3

Parents
Odd Children


Child 2

Parents
Even Children


Child 1

Parents
Odd Children


Please do vote up, and/or click as the answer @Kenny (if your still active around here) if this helps y'all!

Also, Kenny, specifically notice how the embedded template is simply the parent in a channel entries loop? This means you should be able to then access your size_chart relationship in the normal, child fashion...

2
  • Brilliant code that saved my bacon. :) However, I'm having an issue with the $res=... line. Is there any way to amend it to only select parent entries from a specific channel?
    – Matthew
    Feb 1, 2016 at 21:20
  • Sorry but this method will not scale well and is inelegant. Putting an embed inside a loop is a ticking timebomb. Putting a channel entries tag inside an embed inside a loop is a recipe for future disaster. Performance will degrade with every new entry that gets added. Jun 22, 2016 at 14:41

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.