Jump to:

23368 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » need help with relationships(solved)

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

Page: 1
Go to End
Author Topic: 677 Views
  • prawnstar
    Avatar
    Community Member
    116 Posts

    need help with relationships(solved) Link to this post

    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
  • prawnstar
    Avatar
    Community Member
    116 Posts

    Re: need help with relationships(solved) Link to this post

    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

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