Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Using one DataObject for several gridfields


Reply


2 Posts   567 Views

Avatar
SilverPeed

Community Member, 12 Posts

12 February 2014 at 3:06am

Edited: 13/02/2014 4:15am

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;
   }
}

Avatar
martimiz

Forum Moderator, 1105 Posts

3 March 2014 at 5:24am

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