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

Manage one-to-one relations on both sides


Go to End


2 Posts   1295 Views

Avatar
derralf

Community Member, 28 Posts

10 March 2016 at 8:18am

Hallo Community,

I'm working with silverstripe for several years now, but to be honest, I don't really understand how to solve this.

i have two DataObjects:

class Competition extends DataObject{
  ...
  private static $has_one = array(
    'Project' => 'Project'
  );
...

and
class Project extends DataObject{
  ...
  private static $belongs_to = array(
    'Competition' => 'Competition.Project'
  );
...

I understand that on Competition there is a DropdownField to set ProjectID.
But how can I set the relation when editing the Project?
It can't be that complicated to add a similar DropdownField in the Project-Form, but I just can't figure out and feel really dumb as I wasn't even able to find examples via google.

any help appreciated,
cheers,
Ralf

Avatar
martimiz

Forum Moderator, 1391 Posts

17 March 2016 at 10:33am

Edited: 17/03/2016 10:34am

I'm not sure, but editing the relation from within the Competition as well as from within the Project seems somewhat odd to me - it could easily lead to complications, where a Project belongs_to a Competion and that Competition has_one other Project, while it's a 1-1 relation??

However, if you wanted, you could maybe manually create a dropdownfield in Project::getCMSFields() showing all 'available ' Competitions, and then from the Projects onAfterWrite() update the Competitition with the selected ID...