Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » saving a has_one relation

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

Page: 1
Go to End
Author Topic: 1834 Views
  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    saving a has_one relation Link to this post

    Hi!

    I am wondering if this is the way to save my has_one relation.
    It works, but it's not a nice way.

    $Website = new Website();
    $form->saveInto($Website);
    $Website->MaintainerID = $Member->ID;
    $Website->write();

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: saving a has_one relation Link to this post

    I'm curious too - I've used $dataobject->setComponent('Maintainer',$Member->ID) I think, but more often for has_many / many_many - but I normally do it like you have posted...

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: saving a has_one relation Link to this post

    What's wrong with saving the has_ones in that way?

  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    Re: saving a has_one relation Link to this post

    There's nothing wrong with it. It works ok, but I was wondering if there is a better/ nicer way to do:

    $Website->MaintainerID = $Member->ID;

    A way in which you don't have to involve the ID. Is there a method in the data object which couples and saves these has_one relations for you?

  • martimiz
    Avatar
    Forum Moderator
    1078 Posts

    Re: saving a has_one relation Link to this post

    At least a DropdownField in the CMS would work in situations where you 'd want to choose from (a limited amount of) available 'has_ones'...

  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    Re: saving a has_one relation Link to this post

    yep that's true. I'm saving Member->ID so checking if Member object exists before saving should also do the trick.
    Guess we'll have to stick to this way of saving the relation.

  • bcc2k
    Avatar
    Community Member
    4 Posts

    Re: saving a has_one relation Link to this post

    It would be great if a future ss version could support something like the following:

    $Website->Maintainer = $Member;

    For now I use the following as a work-around:

    $Website->Maintainer = $Member;
    $Member->Websites()->add($Website);

    but this hack doesn't work of course if you have just one-way many to one (unidirectional).

    best regards
    andi

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: saving a has_one relation Link to this post

    bcc2k any feature requests should go to open.silverstripe.org. I think having it behave like that would be a practical feature. Not sure of the implications or why it doesn't do it currently.

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