Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » has_many / has_one relationship not correctly setup?

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

Page: 1
Go to End
Author Topic: 652 Views
  • nzblue_fish
    Avatar
    Community Member
    13 Posts

    has_many / has_one relationship not correctly setup? Link to this post

    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

  • nzblue_fish
    Avatar
    Community Member
    13 Posts

    Re: has_many / has_one relationship not correctly setup? Link to this post

    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

  • martimiz
    Avatar
    Forum Moderator
    1068 Posts

    Re: has_many / has_one relationship not correctly setup? Link to this post

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

  • nzblue_fish
    Avatar
    Community Member
    13 Posts

    Re: has_many / has_one relationship not correctly setup? Link to this post

    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

  • nzblue_fish
    Avatar
    Community Member
    13 Posts

    Re: has_many / has_one relationship not correctly setup? Link to this post

    Worked great .. thanks martimiz

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