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.

Data Model Questions

One-to-one relations with ModelAdmin


Reply

5 Posts   1360 Views

Avatar
Judge

28 February 2010 at 12:41am (Last edited: 28 February 2010 11:21am), Community Member, 79 Posts

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
Avatar
Ingo

25 March 2010 at 10:37pm Forum Moderator, 801 Posts

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)); }}

Avatar
Judge

25 March 2010 at 10:42pm (Last edited: 25 March 2010 10:42pm), Community Member, 79 Posts

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

Avatar
Ingo

25 March 2010 at 10:51pm Forum Moderator, 801 Posts

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

Avatar
Judge

26 March 2010 at 1:42am Community Member, 79 Posts

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