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

How to: Multiple has_one of the *same* class containing a has_many


Reply


2 Posts   2128 Views

Avatar
lise

Community Member, 47 Posts

13 December 2011 at 7:34am

Edited: 13/12/2011 7:38am

I have used nested DOM before following the very useful video:

[url]http://www.leftandmain.com/silverstripe-screencasts/2010/08/23/nested-dataobjectmanager-a-dom-in-a-dom/[/url]
of UncleCheese (btw: Thank you very much for it! ) but I would like to implement something slightly different and I think I am reaching the limit of my understanding of SS. Could someone help me with the following?

I want to implement a multi-column footer, each column being a DataObject with a header (text) and a list of DataObjects ( URLName / URLLink ). For various reasons I do not want to implement this in the SiteTree (as a hierarchy of pages ) but as one unique "Footer" page object.

class Footer extends Page {
..
static $has_one = array (
'Column1' => 'FooterSubmenu',
'Column2' => 'FooterSubmenu'
);
..
}

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

   static $has_one = array (
'FooterColumn1' => 'Footer',
'FooterColumn2' => 'Footer',
);

//see [url]http://www.silverstripe.org/data-model-questions/show/18329[/url] for an explanation on the relation above

static $has_many = array (
      'RecLinks' => 'RecLink'
   );
}

class RecLink extends DataObject
{
   static $db = array (
      'LinkName' => 'Varchar(30)',
      'LinkUrl' => 'Varchar(100)',
      
   );

   static $has_one = array (
      'DataObject' => 'DataObject'
   );

   public function getCMSFields_forPopup()
   {
      return new FieldSet(
       new TextField('LinkName'),
       new TextField('LinkUrl')
         
      );
   }
}

And here is how I try to create a Footer page in the CMS :

class Footer extends Page {
..
public function getCMSFields()
   {
      $f = parent::getCMSFields();
      $f->removeFieldFromTab('Root.Content.Main', 'Content');
      $f->addFieldToTab("Root.Content.Column1", new TextField('Column1.Header'));
      $f->addFieldToTab("Root.Content.Column1", new DataObjectManager(
         $this,
         'Column1',
         'FooterSubmenu',
         array('LinkName' => 'LinkName','LinkUrl'=>'LinkUrl'),
         'getCMSFields_forPopup'
      ));
      $f->addFieldToTab("Root.Content.Column2", new TextField('Column2.Header'));
      $f->addFieldToTab("Root.Content.Column2", new DataObjectManager(
         $this,
         'Column2',
         'FooterSubmenu',
         array('LinkName' => 'LinkName','LinkUrl'=>'LinkUrl'),
         'getCMSFields_forPopup'
      ));
      return $f;
   }
..
}

But this does no work because I guess of the 'Column1.Header, 'Column2.Header'

Could someone put me in the right direction, please. I am obviously stuck and...clueless...:-)

Thanks

Avatar
Sticks

Community Member, 31 Posts

15 December 2011 at 11:46am

This is a little different to my linked problem, since mine had a has_many relationship. Are the SubMenus ever going to be in both a Column1 and a Column 2 at the same time? If you are only going to have the one Footer for the entire site, there is no need for the two has_one relationships in the FooterSubmenu class.

static $has_one = array (
'FooterColumn1' => 'Footer',
'FooterColumn2' => 'Footer',
);

With the above, you're essentially saying each FooterSubmenu has two footers. In one footer it is in the Column1, and in the other footer it's in the other column. I assume the Submenu will be in one or the other. Removing one of the above relations will help.

The Column1.Header text field needs to either go in the Submenu class where you have declared the Header in the $db array, or you need to declare a Column1Header in the $db array of the Footer class. Either way you need to remove the '.' in the middle, it's unnecessary.