Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Updating the data in a $has_one relationship inside the CMS

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 3359 Views
  • Ray
    Avatar
    Community Member
    5 Posts

    Updating the data in a $has_one relationship inside the CMS Link to this post

    http://pastie.org/250753

    Hallo all,

    Been bashing my head against this and other problems in SS for a while now, thought i'd ask the wider community.

    In the linked code, (unnecessary .. i think.. bits excluded), the dropdown box gets the list of offices just fine, but it won't update when I click save. It says it all saves ok, but when i navigate away and back again, nothing has changed. What am I missing?

    Investigating the generated code it seems like the <input> tags are fine. I'm stumped.

  • Matt
    Avatar
    Core Development Team
    84 Posts

    Re: Updating the data in a $has_one relationship inside the CMS Link to this post

    Your DropdownField should have 'myOfficeID' instead of 'myOffice', because that's whats generated at the database level

    Also, I should mention that it's common practice to use UpperCamelCase for field names - so it would be something like MyOffice => Office in your $has_one array, and new DropdownField("MyOfficeID").

    Matt.

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Updating the data in a $has_one relationship inside the CMS Link to this post

    try change the DB field you point the drop down to from the has_one name to the actual field in the database eg change the new DropdownField() bit to

    new DropdownField(
             'myOfficeID',
              'Office',
              $OfficesSource,
              $this
          ),

    and see if that saves the data.

    EDIT: matt pretty much posted the same thing just before me.

  • Ray
    Avatar
    Community Member
    5 Posts

    Re: Updating the data in a $has_one relationship inside the CMS Link to this post

    Thanks very much people,

    Works a treat. Here's hoping that little gem of info will trickle down to the rest of my troubles.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Updating the data in a $has_one relationship inside the CMS Link to this post

    Even better, you can use TypeDropdown instead of DropdownField. This means you won't have to do a DataObject::get('Office') to return all the instances of the Office class, then transform that into an array map for the DropdownField source.

    e.g:

    new TypeDropdown('MyOfficeID', 'Office', 'Office');

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