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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

One-to-one relations with ModelAdmin


Go to End


5 Posts   1933 Views

Avatar
Judge

Community Member, 79 Posts

28 February 2010 at 12:41am

Edited: 28/02/2010 11:21am

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

Forum Moderator, 801 Posts

25 March 2010 at 10:37pm

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

Community Member, 79 Posts

25 March 2010 at 10:42pm

Edited: 25/03/2010 10:42pm

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

Forum Moderator, 801 Posts

25 March 2010 at 10:51pm

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

Community Member, 79 Posts

26 March 2010 at 1:42am

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