Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Makings items unique per page: DataObjectManager

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: 796 Views
  • Deklin
    Avatar
    Community Member
    16 Posts

    Makings items unique per page: DataObjectManager Link to this post

    I want each page to contain a list of links, specific to that page. The problem with my code is that is displays the same links on every page.

    Each set of links should only be displayed on the page that it was created for.

    Page.php

    $fields->addFieldToTab("Root.Content.Links", new DataObjectManager(
             $this,
             'FooterMenuItem',
             'FooterMenuItem',
             array('Link'=>'Link','Text'=>'Text'),
             'getCMSFields_forPopup'
       ));

    FooterMenuItem.php

    class FooterMenuItem extends DataObject
    {
       static $db = array (
          'Link' => 'Varchar',
          'Text' => 'Varchar'
       );

       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Link'),
             new TextField('Text')
          );
       }
    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: Makings items unique per page: DataObjectManager Link to this post

    If you're going to be using the DataObjectManager on subclasses of the object where it is defined, you need to use setParentClass($parent) (in your case, "Page")..

  • Deklin
    Avatar
    Community Member
    16 Posts

    Re: Makings items unique per page: DataObjectManager Link to this post

    Thanks, where does the setParentClass code go? Do I need to need to change this to has_one or has_many also?

    //Page.php

    $manager = new DataObjectManager(
             $this,
             'FooterMenuItem',
             'FooterMenuItem',
             array('Link'=>'Link','Text'=>'Text'),
             'getCMSFields_forPopup'
       )

    $manager->setParentClass('Page');

    $fields->addFieldToTab("Root.Content.Links", $manager);

    //FooterMenuItem.php
    class FooterMenuItem extends DataObject
    {
       static $db = array (
          'Link' => 'Varchar',
          'Text' => 'Varchar'
       );

       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Link'),
             new TextField('Text')
          );
       }
    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: Makings items unique per page: DataObjectManager Link to this post

    Make sure Page has $has_many FooterMenuItem and FooterMenuItem $has_one Page.

  • Deklin
    Avatar
    Community Member
    16 Posts

    Re: Makings items unique per page: DataObjectManager Link to this post

    Thanks but I am still at a loss. How do I make sure Page has $has_many FooterMenuItem and FooterMenuItem $has_one Page?

    My code is posted in my previous link.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Makings items unique per page: DataObjectManager Link to this post

    Have you read the DOM documentation? You need to set up a relationship between Page and FooterLinks.

    Page:

    static has_many = array (
    'FooterLinks' => 'FooterLink'
    );

    FooterLink:

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

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