Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

How to manage two many_many relationships with each other?

Go to End

7 Posts   1612 Views


Community Member, 167 Posts

8 January 2009 at 4:05pm

Edited: 08/01/2009 4:07pm

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 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...


Administrator, 690 Posts

9 January 2009 at 8:35am

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.


Community Member, 167 Posts

9 January 2009 at 2:18pm

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?



Community Member, 712 Posts

9 January 2009 at 5:34pm

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.


Community Member, 167 Posts

9 January 2009 at 7:36pm

Edited: 12/01/2009 9:08pm

Hmm, partly...

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


Community Member, 167 Posts

10 January 2009 at 2:24am

Edited: 12/01/2009 9:16pm

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 :)


Community Member, 167 Posts

12 January 2009 at 9:15pm

Edited: 12/01/2009 9:18pm

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):

- Ray.