Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » How to: Multiple has_one of the *same* class containing a has_many

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: 1993 Views
  • lise
    Avatar
    Community Member
    47 Posts

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

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

    http://www.leftandmain.com/silverstripe-screencasts/2010/08/23/nested-dataobjectmanager-a-dom-in-a-dom/
    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 http://www.silverstripe.org/data-model-questions/show/18329 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

  • Sticks
    Avatar
    Community Member
    29 Posts

    Re: How to: Multiple has_one of the *same* class containing a has_many Link to this post

    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.

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