Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Using one DataObject for several gridfields

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: 473 Views
  • SilverPeed
    Avatar
    Community Member
    12 Posts

    Using one DataObject for several gridfields Link to this post

    Hallo somebody,

    I have a question about using a DataObject several times on one page with Silverstripe 3.2.1

    In my footer i have several boxes with several Items.
    I would like to add these several items thru a GridField

    So you could have GridField1 for Box1 and GridField2 for Box2 etc.

    I made a DataObject FooterText and made a has_one relation to HomePage.
    On my HomePage i make several Variables from the DatabaseObject type FooterText.

    The problem is that when i put text in one GridField it appears in all of the GridFields.
    There is no separation between Blok1, Blok2 etc. it is all one DataObject.
    Howe could i fix this?

    See my code below

    ------------------------------------------------
    <?php
    class FooterText extends DataObject
    {
    Private static $db = array (
    'Title' => 'Varchar',
    'URL' => 'Varchar',
    );

    Private static $has_one = array (
    'ConnectionToHomePage' => 'HomePage',
    );
       
        public static $summary_fields = array(
        'Title' => 'Title'
    );

    public function getCMSFields() {
          $fields = parent::getCMSFields();

          $fields->removeFieldFromTab("Root.Main","ConnectionToHomePageID");
          $fields->removeFieldFromTab("Root.Main","SortOrder");
          return $fields;      
    }
    }
    ------------------------------------------------
    <?php
    class HomePage extends Page {
    public static $has_many = array(
          'Blok1' => 'FooterText',
          'Blok2' => 'FooterText',
          'Blok3' => 'FooterText',
          'Blok4' => 'FooterText'
       );
    public function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields-> addFieldToTab('Root.Footer', new GridField('Blok1', 'Footer Blok 1', $this->Blok1(), GridFieldConfig_RelationEditor::create()));
          $fields-> addFieldToTab('Root.Footer', new GridField('Blok2', 'Design Tips', $this->Blok2(), GridFieldConfig_RelationEditor::create()));
          $fields-> addFieldToTab('Root.Footer', new GridField('Blok3', 'Voorwaarden', $this->Blok3(), GridFieldConfig_RelationEditor::create()));
          $fields-> addFieldToTab('Root.Footer', new GridField('Blok4', 'Producten', $this->Blok4(), GridFieldConfig_RelationEditor::create()));
          return $fields;
       }
    }

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: Using one DataObject for several gridfields Link to this post

    Sorry for the late reaction. This DataObjectManager section deals with uncle cheese's DatObjectManager module, that only supports ss2.4, so...

    The problem with your setup is that each relation ID is stored in the FooterText.ConnectionToHomePageID field, and there is no way for the GridField to know which Block it belongs to. This basically means that you cannot have more then one has_many link to the same DataObject. What you probably could do is create three different DataObjects, all extending Footertext as empty wrappers:

    FooterBlock1 extends Footertext {} and so forth

    Or you could use one single gridfield and add a belongsToBlock enum dropdown to the footertext object...

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