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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

has_many / has_one relationship not correctly setup?


Reply


5 Posts   761 Views

Avatar
nzblue_fish

Community Member, 13 Posts

14 January 2012 at 5:42pm

Hi people,

I'm building my first SilverStripe website. Things are going pretty well so far, but I've run into a design issue that has me momentarily stumped. I'm hoping some kind SilverStriper will help me out so I can get on with building the site.

I have a Page object (GymClass - which describes a certain type of class being run by the gym) for which I have defined a 1-m relationship to a ScheduledGymClass DataObject (describing the days and time slots a particular gym class occur on).

The ScheduledGymClass has a 1-1 relationship with a GymClass since for a particular timeslot only one occurance of a given gym class is permitted, but of course the gym class can occur on multiple days and at other times.

What I want to end up with is, for each GymClass, a list of the days and timeslots on which it occurs.

GymClass has a $has_many relationship with ScheduledGymClass which has a reciprocal $has_one relationship with GymClass.

Checking in the database I can see the ScheduledGymClass table has a column defining the $has_one relationship (PlannedGymClass).

However, when I add a new ScheduledGymClass entry for a defined GymClass Page (via a HasManyComplexTableField on GymClass) the value of the supposed $has_one field in ScheduledGymClass is set to 0 and not the ID of the related GymClass row.

Also if I add another GymClass Page, the ScheduledGymClass entries for any previously saved ScheduledGymClass entries also appear even though they don't relate to that particular GymClass.

Have I missed something in my design. Shouldn't saving the ScheduledGymClass object capture the ID of the related GymClass object because of the defined relationship.

Any help would be gratefully received.

Cheers, Innes

Avatar
nzblue_fish

Community Member, 13 Posts

14 January 2012 at 7:22pm

Problem solved, but suggestions or comments always welcome.

1) Mental note to self: Remember to link (check) the has_many side of the relationship to establish the link. A trap for new players perhaps in that the relationship Popup only creates the related object row and not the relationship until you use the checkbox.

2) Use the filter parameter on the HasManyComplexTableField object to limit the display to the rows with the linked object ID or 0 (an as yet unlinked row).

More work to do on this but at least I understand it now.

Cheers, Innes

Avatar
martimiz

Forum Moderator, 1132 Posts

15 January 2012 at 6:28am

Hi

You could consider using the ComplexTableField instead of the HasManyComplexTableField. The default ComplexTableField will only display the objects that are linked to the current GymClass page - and it will automatically link every new object you create to the the currentpage as well. No filtersetting needed...

The checkbox variations are actually ment to give that opportunity to select which object you want to belong to the current page, out of the entire range of objects. In some situations that might come in handy, though has_many's can of course never be shared between pages...

Hope that explains it some more...

Avatar
nzblue_fish

Community Member, 13 Posts

15 January 2012 at 10:47am

Thanks for the alternative suggestion. I'll have a play with this approach and see how it works for this UseCase.

As always, it's both exciting and frustrating to learn a new framework. So much yet to uncover with SilverStripe.

Cheers,
Innes

Avatar
nzblue_fish

Community Member, 13 Posts

15 January 2012 at 8:48pm

Worked great .. thanks martimiz