Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » $has_one / $has_many DataObject to DataObject

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 825 Views
  • Garrett
    Avatar
    Community Member
    245 Posts

    $has_one / $has_many DataObject to DataObject Link to this post

    Hi,

    I am used to creating a $has_many -> $has_one relationship from a Page to a DataObject, however that's not what I want this time and I am having trouble getting the Parent ID to save into the Child table. Here is the parent object:

    [pre]

    class Service extends DataObject {
       
       ....
       
       static $has_one = array(
          "ServiceHolder" => "ServiceHolder"      
       );

       static $has_many = array(
          "SubServices" => "SubService"
       );

       ....

       function getCMSFields_forPopup() {

          $fields = new FieldSet();
          
          $fields->push(new TextField("ServiceName", "Name of Service"));
          $fields->push(new SimpleTinyMCEField("ServiceDescription", "Description"));

          return $fields;

       }
       
    }


    [/pre]

    And here is my child object:

    [pre]

    class SubService extends DataObject {
       
       ....
       
       static $has_one = array(
          "Service" => "Service"      
       );

       ....

       function getCMSFields_forPopup() {

          $fields = new FieldSet();
          
          $fields->push(new TextField("SubServiceName", "Name of Service"));
          $fields->push(new SimpleTinyMCEField("SubServiceDescription", "Description"));
          
          $services = DataObject::get("Service");
          $services_dropdown = new DropdownField("Service", "Parent Service", $services->toDropDownMap("ServiceID","ServiceName"));
          $fields->push($services_dropdown);

          return $fields;

       }
       
    }


    [/pre]

    What I am trying to do here is to allow the user to choose which Service each SubService belongs to. When you create/edit a SubService, I want to be able to choose the Parent Service from a dropdown list. However the result of this code is such that I see a dropdown which contains only one Service -- the first one -- that which has an ID of 0.

    Firstly, why will my dropdown not show all the Services? What am I doing wrong? How do I save the value of the selected Parent into the child object?

    Thank you in advance,
    Garrett

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: $has_one / $has_many DataObject to DataObject Link to this post

    $services_dropdown = new DropdownField("Service", "Parent Service", $services->toDropDownMap("ServiceID","ServiceName"));

    That 'Service' $name parameter has to be ServiceID

    $services_dropdown = new DropdownField("ServiceID", "Parent Service", $services->map());

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