Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » One-to-one relations with ModelAdmin

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

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

    One-to-one relations with ModelAdmin Link to this post

    Using the ModelAdmin I would like to implement one-to-one relations, but am unsure how to tell ModelAdmin the way to represent it.

    The partial data model is:

    Resumes --< Education +--- Address

    So a resume could have many education records, and each of those records may have a single address of the establishment (university, school, college, etc.).

    Using the standard ModelAdmin screens, the Education record is edited in a pop-up. However, the address is then selected in a drop-down of addresses, which effectively implements a one-to-many (addresses to education records). How would I go about making this one-to-one, perhaps having the address as a second tab on the education pop-up? Since there is a "Main" tab there, I assume it is possible to add further tabs.

    How would saving the data then work? Would both records (education and address) be submitted and saved at the same time? What about clashes of field names - would the "Name" of the Address get mixed up with the "Name" of the education record?

    Attached: screen shot of the education tab, showing the address drop-down that should actually be a single form.

    -- Jason

    Attached Files
  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: One-to-one relations with ModelAdmin Link to this post

    http://open.silverstripe.org/ticket/4034
    http://open.silverstripe.org/changeset/90072

    This is in trunk, but you can easily work around it in 2.4 already by defining a $has_one on one side, and doing a custom getter on the other side.

    class Education { static $has_one = array('Address' => 'Address'); }

    class Address { function getEducation() { return DataObject::get_one('Education', sprintf('"AddressID" = %d', $this->ID)); }}

  • Judge
    Avatar
    Community Member
    79 Posts

    Re: One-to-one relations with ModelAdmin Link to this post

    That's nice and simple. I did find a slightly more involved solution to this on 1.3 and posted it on another thread.

    However, I can't find that thread now - any idea how to display all the threads started by an individual? My profile page only shows the last ten.

    -- Jason

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: One-to-one relations with ModelAdmin Link to this post

    Don't think there's a way for displaying all threads by person, but perhaps @willr can work his magic if you file a well-described ticket for it on http://open.silverstripe.org/query?status=inprogress&status=new&status=replyneeded&status=reviewed&component=Modules+-+forum&order=priority&col=id&col=summary&col=status&col=type&col=priority&col=milestone&col=component

  • Judge
    Avatar
    Community Member
    79 Posts

    Re: One-to-one relations with ModelAdmin Link to this post

    Feature request duly raised.

    And here is the previous thread that shows how to include a 1:1 data object as a second tab in the parent data object, and have both saved at the same time when the user submits the form:

    http://www.silverstripe.org/general-questions/show/279948

    -- Jason

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