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.

Customising the CMS

How to manage two many_many relationships with each other?


Reply

7 Posts   1260 Views

Avatar
SilverRay

8 January 2009 at 4:05pm (Last edited: 8 January 2009 4:07pm), Community Member, 167 Posts

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

Avatar
Sam

9 January 2009 at 8:35am Administrator, 685 Posts

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.

Avatar
SilverRay

9 January 2009 at 2:18pm Community Member, 167 Posts

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!

Avatar
Hamish

9 January 2009 at 5:34pm Community Member, 712 Posts

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.

Avatar
SilverRay

9 January 2009 at 7:36pm (Last edited: 12 January 2009 9:08pm), Community Member, 167 Posts

Hmm, partly...

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

Avatar
SilverRay

10 January 2009 at 2:24am (Last edited: 12 January 2009 9:16pm), Community Member, 167 Posts

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

Avatar
SilverRay

12 January 2009 at 9:15pm (Last edited: 12 January 2009 9:18pm), Community Member, 167 Posts

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.