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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

need help with relationships(solved)


Reply

2 Posts   698 Views

Avatar
prawnstar

22 July 2010 at 7:33am (Last edited: 23 July 2010 8:17am), Community Member, 116 Posts

EDIT: cleaned up the code for easier reading.

I'm trying to make a set of pages that contain multiple slideshows.

It's supposed to look basically like this...Images have a many to one relationship with a slideshow and a slideshow has a many to many relationship across several pages/types.

I've set up a templateless class to house my slideshows, but I'm missing how exactly to access these slideshows and display them on their related template. It would seem I have the relationships correct as I can create a slideshow in teh holder and from the pages that I want the slideshow to appear on, I can select that slideshow in the cms. See screenshots. I'm just not sure how to display them.

Thanks a ton, to anyone that can take a look at this!

Here is the code for the Images and Slideshow:

/mysite/code/SlideImage.php

class SlideImage extends DataObject {
   static $db = array(
      'Label' => 'Text',
      'SlideOrder' => 'Text'
   );
   static $has_one = array('Image' => 'Image');
   static $belongs_many_many = array('SlideshowBuckets' => 'SlideshowBucket');
   function getCMSFields_forPopup() {
      $fields = new FieldSet();
      $fields->push( new TextField('Label'));
      $fields->push( new TextField('SlideOrder'));
      $fields->push( new ImageField('Image'));
      return $fields;
   }
}

/mysite/code/SlideshowBucket.php

class SlideshowBucket extends Page {
   static $allowed_children = array();
   static $db = array(
      'Headline' => 'Text',
      'Copy' => 'Text',
      'Link' => 'Text'
   );
   static $many_many = array('SlideImages' => 'SlideImage');
   static $belongs_many_many = array(
      'WorkContentDetailPages' => 'WorkContentDetailPage',
      'WorkHomePages' => 'WorkHomePage'
   );
   function getCMSFields() {
      $fields = parent::getCMSFields();
      $fields->addFieldToTab('Root.Content.Main', new TextField('Headline'));
      $fields->addFieldToTab('Root.Content.Main', new TextareaField('Copy'));
      $fields->addFieldToTab('Root.Content.Main', new TextField('Link'));
      $fields->removeFieldFromTab('Root.Content.Main','Content');
      $modulesTablefield = new ManyManyComplexTableField(
         $this,
         'SlideImages',
         'SlideImage',
         array(
            'Label' => 'Image Label',
            'SlideOrder' => 'Image Order',
            'Image' => 'Image Preview'
         ),
         'getCMSFields_forPopup'
      );
      $modulesTablefield->setAddTitle( 'An Image' );
      $fields->addFieldToTab('Root.Content.SlideImage',$modulesTablefield);
      return $fields;
   }
}

And finally, this is the pages that I want my slideshows to appear on:

/mysite/code/WorkContentDetailPage

class WorkContentDetailPage extends Page {
   static $allowed_children = array('SlideshowBucket');
   static $many_many = array('SlideshowBuckets' => 'SlideshowBucket');
   function getCMSFields() {
      $fields = parent::getCMSFields();
      $modulesTablefield = new ManyManyComplexTableField(
         $this,
         'SlideshowBuckets',
         'SlideshowBucket',
         array(
            'Title' => 'Title',
            'Headline' => 'Headline'
         ),
         'getCMSFields_forPopup'
      );
      $modulesTablefield->setAddTitle( 'A Slideshow Module' );
      $fields->addFieldToTab('Root.Content.SlideshowBucket',$modulesTablefield);
      return $fields;
   }
}
class WorkContentDetailPage_Controller extends Page_Controller {}

Attached Files
Avatar
prawnstar

22 July 2010 at 10:24am (Last edited: 23 July 2010 8:17am), Community Member, 116 Posts

So...

Adding a function to the page controller to actually call the dataobject was helpful:

   function workSlidesBucket() {
      return DataObject::get("SlideshowBucket", "xxxxx", "xxxxx");
   }

EDIT:
Additionally, I was using <% control children %> instead of using <% control SlideshowBuckets %> in my template file.
Hope this helps someone.

Chris.b