Jump to:

3431 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » One child object, many potential parent objects

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

Page: 1
Go to End
Author Topic: 1276 Views
  • Judge
    Avatar
    Community Member
    79 Posts

    One child object, many potential parent objects Link to this post

    I have a Resume data object with a number of detail data objects - Employment, Education, Competencies. These are all one-to-many and straight-forward to code. Each detail object will always have one master Resume object.

    I also have an Addresses data object, which is linked to a Resume, and then optionally to any of the detail objects, e.g. the address of an employer (Employment), the address of a school (Education), etc. Each of the detail objects may optionally point to one address.

    I am doing this so that all the addresses can be handled in the same way and stored in the same database table for convenience. What is the best way to model this? Each Address object will have a parent that could be a Employment record *or* an Education record.

    Would I just set $has_one(Address) for each detail object (Employment, Education, etc) but ignore the relationship in the opposite direction, handling it in code instead?

    -- Jason

  • Judge
    Avatar
    Community Member
    79 Posts

    Re: One child object, many potential parent objects Link to this post

    Okay, let me put it another way:

    I have three data objects (A, B and C), all with a common parent. That makes them all related. So far so good.

    Now each of those three data objects has an optional one-to-one relationship with another data object (Address). Any one of those three objects could have its own Address object; each Address object has just one owner, i.e. it is a detail of just one of the data items in one of the three objects.

    From an administrator's point of view, when editing any of A, B or C, the fields from Address should be tagged on the bottom of the form. It should look like the Address is a part of A, B or C, even though the address is stored in a separate data store.

    Is there a simple way to achieve that? If the address details need to be put into a pop-up form, fair enough (just for the admin interface to the raw data, which is just there for me to fix problems with the data), but that relationship one-to-one puts the relation the opposite way around to the usual master/detail mixed forms.

    Does that make sense?

    The relations are:

    Master ---< A
    Master ---< B
    Master ---< C
    A +--- Address
    B +--- Address
    C +--- Address

    -- Jason

    PS How about markdown support for the forums? Markdown is very intuitive, and reads well even before it is transformed.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: One child object, many potential parent objects Link to this post

    "Would I just set $has_one(Address) for each detail object (Employment, Education, etc) but ignore the relationship in the opposite direction, handling it in code instead?"

    Yeah, that's probably your best bet. Sapphire doesn't handle one-one relations very well (you end up with what looks like two broken one-many pairs), so it is usually cleaner to do as you say. From your first post I think you are on the right track.

    "PS How about markdown support for the forums? Markdown is very intuitive, and reads well even before it is transformed."

    +1. BBCode is a bit 2002, right?

  • Judge
    Avatar
    Community Member
    79 Posts

    Re: One child object, many potential parent objects Link to this post

    I've been spoiled a bit using markdown in other projects, and love it. I'm not sure if is does the N-level nested quotes like bbcode does, but perhaps that is a good thing.

    Thanks for the feedback. I can carry on the line I'm following, knowing that I haven't missed out some obvious functionality of SS or Sapphire.

    -- Jason

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