Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » How to manage two many_many relationships with each other?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1240 Views
  • SilverRay
    Avatar
    Community Member
    167 Posts

    How to manage two many_many relationships with each other? Link to this post

    OK, this might be an interesting one, perhaps I'm not seeing it because I over-think it. Just suppose I have a product page class, Product.php and its template Product.ss. For simplicity's sake, let's assume the product displayed has two properties, a color and a availability status. So to set up the structure to manage colors per product page, I create a dataobject called color, set up a many_many relationship, display it in a separate tab in the CMS, you know, the works. I can now "attach" colors to each product page, and have it iterated over in the template by using a control block. I can do the same for the availability status. But what if the availability status is color dependent? So, for instance, in a particular product page I have three colors set up (red, green, and blue), but only two of them are available? In other words, how can I relate the "color many_many" to the "availability many_many"? So to speak, I envision a ManyManyComplexTableField of some sorts to handle two many_many relations, as if it would have two independent columns of checkboxes, if you catch my drift. Is there a way to do this?

    Thanks for thinking with me on this one, maybe I overlook the obvious...

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: How to manage two many_many relationships with each other? Link to this post

    Your data model won't support the kind of structure you're looking for.

    You'll probably want to create a ProductType record, which records one colour-availability pair:

    * Product has_many ProductType
    * ProductType has_one Product
    * ProductType has_one Color
    * ProductType has_one Availability

    You can then create a TableField, listing all of the ProductTypes linked to a Product, that has a dropdown field for Color and Availability.

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: How to manage two many_many relationships with each other? Link to this post

    Hi Sam,

    Thanks for your suggestion! But: doesn't this limit me to one color-availability pair per Product page? In other words, because it is a has_many situation, if I select for instance "blue-available" in one Product page, I can not select that one anymore in another Product page?

    Thanks!

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: How to manage two many_many relationships with each other? Link to this post

    No, a has_one relationship with one obejct doesn't exclude another object having an has_one object with the object.

    Hopefully that made sense.

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: How to manage two many_many relationships with each other? Link to this post

    Hmm, partly...

    Edited: Removed code, I put wrong code here, see later post.

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: How to manage two many_many relationships with each other? Link to this post

    Hmm, wait, perhaps I misunderstood: did you actually mean that every row in the tablefield will contain a dropdown? That would actually help me, I think...

    Edit: that's not what you meant indeed and also what -of course- would not be the solution

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: How to manage two many_many relationships with each other? Link to this post

    OK, so I think I misunderstood Sam's post (sorry Sam), but I thought of another (perhaps temporary) solution. Because there's a limited amount of colors to choose from, I just made one Color dataobject, with an extra boolean "Status" field. I then just batch upload a double list of colors in the corresponding database table, half of which have the status field set to 1 and the other half to 0, so the CMS-user can choose between an available-color and the-same-but-not-available-color. No biggie. I think my original post was not so clear perhaps.

    Still, also in this case I'm kind of suffering from the problem I posted here (code posted does not reflect the above new situation yet, but same results nevertheless):

    http://www.silverstripe.org/customising-the-cms/show/252082?showPost=252082

    Thanks,
    - Ray.

    1240 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.