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.

Many to Many Relationships in ExpressionEngine 2.6??

June 19, 2013 12:58pm

Subscribe [4]
  • #1 / Jun 19, 2013 12:58pm

    ryandavidson.dk

    4 posts

    I’ve dug through Google extensively and cannot find a clear, concise answer for this.
    ExpressionEngine is powerful, but this one glaring blind spot is almost enough to look elsewhere when choosing a framework…

    My Problem
    I’ve got a Products channel. I want to allow Products to be related to other Products in a bi-directional, many-to-many way. In Rails terms this would be a has_and_belongs_to_many relationship, in SQL it’d be a JOIN table. Essentially I need to link/bind the two entries together, neither being the “parent” or “child” of the either — and I need to do it in a way where I’m defining the relationship once, not twice (relating A => B should implicitly relate B => A).

    Example scenario
    Given that... Product entries have a related_products field (relationship? playa? matrix?)
    Given that... There are two Product entries: Guitar Strings and Guitar Strings
    When I... Edit the Guitar entry’s related_products field to include Guitar Strings entry
    I expect... The Guitar Strings entry’s related_products to now include Guitar Strings entry
    So That... In templates, Guitar:related_products returns Guitar Strings, and conversely Guitar Strings:related_products returns Guitar

    So… WTF?
    I purchased Playa and Matrix, the two recommended add-ons for “complex” data relation problems. But I’m extremely disappointed that they don’t intuitively solve this.

    Am I missing something? The old documentation for Playa shows a reverse_related_entries tag — that sounds/looks like exactly what I need… but it was deprecated since EE2.0

    Do I need to create a new channel called “Product Relation”, with just 2 Single Select Playa fields, so I can lookup all relationships that include any given Product? This seems like a highly inelegant solution.

    Any guidance would be much appreciated… this may be a show stopper for me in the future when I think about using EE for my sites

  • #2 / Jun 19, 2013 3:34pm

    ryandavidson.dk

    4 posts

    Follow up after more experimentation with Playa.

    I’m officially baffled.

    Here’s the template code I’m using to generate the results in the attached screenshots (within a {exp:channel:entries channel="product"} loop):

    <td>
        <b>All Related</b>:
        {related_products:ol}
    </td>
    <td>
        <b>Parents</b>: {exp:playa:total_parents}
    
        <ul>
        {exp:playa:parents field="related_products"}
            <li>{title}</li>
        {/exp:playa:parents}
        </ul>
    </td>
    <td>
        <b>Children</b>: {exp:playa:total_children}
    
        <ul>
        {exp:playa:children field="related_products"}
            <li>{title}</li>
        {/exp:playa:children}
        </ul>
    </td>
    <td>
        <b>Siblings</b>: {exp:playa:total_siblings}
    
        <ul>
        {exp:playa:siblings field="related_products"}
            <li>{title}</li>
        {/exp:playa:siblings}
        </ul>
    </td>
    <td>
        <b>Co-Parents</b>: {exp:playa:total_coparents}
    
        <ul>
        {exp:playa:coparents field="related_products"}
            <li>{title}</li>
        {/exp:playa:coparents}
        </ul>
    </td>


    Questions / Illogical Findings:
    1 — Why do entries show 1 Child and 1 Co-parent when there are no relationships defined? Is an entry inherently a child of itself, and thus a co-parent of itself too? Further evidenced by Wheel having 1 Sibling when it is the only child of Car

    2 — The “All Related” list is displayed via {related_products:ol} (a Playa field). Why does Wheel list Car as a parent via {exp:playa:parents}, but not as a generally related entry? I expected any relationship (parent || child || sibling || coparent) to be pulled in, but this makes sense knowing the relationship isn’t reciprocated automagically (like I expected).

    3 —  In my test with 3 entries, I don’t understand why Car has 2 Co-parents and Motorcycle has 1 Co-parent, when both have Wheel as their only child. The numbers don’t make sense.

  • #3 / Jun 19, 2013 4:16pm

    Brandon Kelly

    257 posts

    Hi Ryan,

    That experimental template code – is that within an {exp:channel:entries} tag pair?

    I’m having a hard time following exactly what relationships you actually have set up. Would be helpful to have a rundown of all existing entries, Playa fields, and which entries are relating to which other entries.

    Sounds like you figured out that {exp:playa:parents} is how you get to reverse-related entries…

    -Brandon

    P.S. For future reference, we have several official support avenues, but these forums are not one of them (per EllisLab’s own preferences). In the future please post your questions in one of the ways suggested here: http://pixelandtonic.com/support

  • #4 / Jun 19, 2013 4:55pm

    Kevin Smith

    4784 posts

    ...per EllisLab’s own preferences…

    Actually, that was an old, unfortunately policy, put into place by folks who are no longer with EllisLab. The new guidelines for the Community Support forum are pretty simple and straightforward:

    We’ve only got a few ground rules. Treat people here like you would face to face. Be courteous, and remember, everyone helping out in this forum is doing so on their own time.

    But yep, it’s always best to check out the add-on makers’ official support avenues to be sure you’ll get the help you need.

    Ryan, just wanted to make sure you knew that the new Relationships fieldtype included in EE 2.6 is pretty powerful and it looks like it’ll suit you nicely. Here’s the documentation for it. You don’t have to use it of course, and it sounds like you’re already working on a solution with Playa, but if you want to use the native fieldtype, we’re happy to help with super quick, private support. 😊

  • #5 / Jun 19, 2013 5:34pm

    ryandavidson.dk

    4 posts

    Thanks for the replies, good info to know.

    I re-read the docs on relationships and am still lost.

    Maybe I’ll rephrase my question this way… everything I’ve found so far regarding relationships is strictly tied to the concept of Parents and Children. Is there a way, add-on or otherwise, to have a simple relationship in the true sense of “these two things are equally related to eachother”. Not a shared parent/sibling, not a co-parent, just a straightforward link/join/bind/connection between two entries?

    The old {reverse_related_entries} tag sounds close to what I am looking for.


    Here’s the short-and-sweet problem I have:

    Our site has Products.
    Products can be related to other products. This is for more granular organization than large categorical bins.
    There’s an existing product entry called “Foo”.
    I add a new product entry called “Bar”. I put “Foo” as a related_product (via relationship field).
    Big question: How do I get “Foo” to be aware or reciprocate “Bar”‘s relationship to it?

    Brandon, {exp:playa:parents} isn’t the right solution here because for maintenance purposes, I don’t want admins to be running around chasing parents/children relationships amongst products. If product “Bar” changes and is no longer related to “Foo”, admins should be able to go to either “Foo” or “Bar” entries and have the ability to remove the relation.

    Make sense?

    As powerful as EE is, this inherent parent/child hierarchy of relationships is a real buzzkill. I thought Playa would solve these woes, and maybe it does and I’m just doing it wrong..

    At this point, I’m thinking about achieving this through a raw SQL query.

  • #6 / Jun 19, 2013 5:44pm

    Brandon Kelly

    257 posts

    You are correct – all relationships created by Playa as well as EE’s native Relationship field have strict parent and child entries. This is because most of the time people want to be able to order the related entries, and you would lose that control if you were able to define relationships from both ends.

    You can very easily list all of an entry’s parents and children in one big list in your templates, so this only really affects the content entry. The usability issue you pointed out can be solved with this fieldtype: http://fromtheoutfit.com/blog/playa-links-for-expressionengine – it gives each entry a read-only list of other entries that are relating to it. With that installed, your authors wouldn’t have to go searching for the other entries that are pointing to the current one; they would all be listed right there, below the Playa field.

    Hope that helps!

  • #7 / Jun 19, 2013 5:49pm

    ryandavidson.dk

    4 posts

    Holy crap, that may be exactly what I’m looking for! I don’t know if I ever would have found that article on my own…

    Thanks Brandon, that may solve the content entry usability issue and let me move past this hurdle.

    I’ll give it a shot and come back with my results.

  • #8 / Sep 12, 2013 9:56am

    C Campbell

    23 posts

    Hi Ryan, I’ve got the same problem here with a site. How did you get on with Playa and the ‘Playa Links’ fieldtype?

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

ExpressionEngine News!

#eecms, #events, #releases