Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » SS3: Edit single DataObject (has_one relationship) in CMS without GridField

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

Page: 1
Go to End
Author Topic: 351 Views
  • vwd
    Avatar
    Community Member
    157 Posts

    SS3: Edit single DataObject (has_one relationship) in CMS without GridField Link to this post

    Hi,

    Is there a way of being able to edit a single DataObject (in a has_one relationship) in the CMS without having to engage GridField (and changing to a has_many relation)?

    I have a DataObject in SiteConfig (has_one) relationship, and would like to be able to edit it.

    Example code:
    DataObject

    class MyImage extends DataObject
    {
       static $db = array (
          'TitleText' => 'Text',
       );

       static $has_one = array (
          'MyImageFile' => 'Image',
       );

       public function getCMSFields()
       {
          return new FieldList(
             new TextField('TitleText'),
             new UploadField('MyImageFile')
          );
       }
    }

    CustomSiteConfig.php

    class CustomSiteConfig extends DataExtension{

       static $db = array();
       
       static $has_one = array(
          'DefaultMyImage' => 'MyImage'
       );
       
       static $has_many = array(
       
       );
       
       function updateCMSFields(FieldList $fields) {
          // What field do I need to include here to be able to edit a single MyImage DataObject?

          return $fields;
       }   
    }

    Is it possible for some kind of field to use MyImage.getCMSFields() to generate the appropriate fields in the CMS? Or is there a way of explicitly adding each member of the DataObject to the CMS e.g.

       // This doesn't seem to work...
       function updateCMSFields(FieldList $fields) {
          $fields->addFieldToTab('Root.MyImage', new TextField('DefaultMyImage.TitleText', 'Title'));
          $fields->addFieldToTab('Root.MyImage', new UploadField('DefaultMyImage.MyImageFile', 'My Image'));
          return $fields;
       }   

    Thanks very much.
    VWD.

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