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

Makings items unique per page: DataObjectManager


Reply

6 Posts   867 Views

Avatar
Deklin

14 July 2010 at 3:20am Community Member, 16 Posts

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

Avatar
UncleCheese

14 July 2010 at 4:20am 4085 Posts

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

Avatar
Deklin

14 July 2010 at 5:33am Community Member, 16 Posts

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

Avatar
UncleCheese

14 July 2010 at 5:49am 4085 Posts

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

Avatar
Deklin

14 July 2010 at 6:35am Community Member, 16 Posts

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.

Avatar
UncleCheese

14 July 2010 at 7:47am 4085 Posts

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'
);