Jump to:

3459 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » $many_many - tracking down the ole relation chain

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

Page: 1
Go to End
Author Topic: 877 Views
  • NickJacobs
    Avatar
    Community Member
    144 Posts

    $many_many - tracking down the ole relation chain Link to this post

    Ok, so I've got a 'Banner' DataObject which has_one 'BannerChooser' PageType, and also belongs_many_many to SiteTree...

    How it works is BannerChooser pages are used to upload banners etc, then on any top level page in the site we can choose which banners are displayed in that section. There are 3 different banner types, and I've set them up on 3 different BannerChooser pages to keep it tidy. What I need to do is filter a ManyManyComplexTableField by the BannerChooser type........
    phew

    Banner.php

    class Banner extends DataObject
    {
       static $db = array (
       'Name' => 'Text'
       );
       
       static $has_one = array (
          'BannerChooser' => 'BannerChooser',
          'Image' => 'Image'
       );   
       static $belongs_many_many = array (
          'Pages' => 'SiteTree'
          );   
       public function getCMSFields_forPopup()
       {
          $fields = new FieldSet();
          $fields->push(new TextField('Name'));
          $fields->push(new ImageField('Image','Upload banner'));      
          return $fields;
       }   
    }

    BannerChooser.php

    class BannerChooser extends Page
    {
       static $db = array (
       'BannerType' => 'Text'   
       );   
       
       static $defaults = array(
          'ShowInSearch' => false,
          'ShowInMenus' => false
          );
       
       static $has_many = array (
          'Banners' => 'Banner'
       );
       public function getCMSFields()
       {
          $f = parent::getCMSFields();      
          
          $manager = new ImageDataObjectManager(
             $this, // Controller
             'Banners',
             'Banner',
             'Image',
             array(
                'Name' => 'Name'
                
             ),
             'getCMSFields_forPopup'
          );
          
          $manager->setParentClass('BannerChooser');
          $manager->setBrowseButtonText("Upload ");      
          $manager->setPluralTitle('Banners');         
          
          $f->addFieldToTab("Root.Content.Main", new OptionsetField("BannerType","What kind of banner is it?",
           $source = array(
              "banner" => "Top Banner",      
              "promo" => "Promo Banner",
              "ad" => "Ad Tower"
           )));   
          
          $f->addFieldToTab("Root.Content.Main", $manager);   
          
          return $f;
       }   
       
    }

    and in Page.php

    class Page extends SiteTree {   
    ...
    static $many_many = array(
    "Banners" => "Banner"
    );   
       public function getCMSFields()
       {
          $f = parent::getCMSFields();      
          if($this->ParentID == 0){      
          $areasTablefield = new ManyManyComplexTableField(
              $this,
              'Banners',
              'Banner',
              array(
             'Name' => 'Name'
              ),
              'getCMSFields_forPopup'
           );
           $areasTablefield->setAddTitle( 'PromoBanners' );
           $f->addFieldToTab( 'Root.Content.BannersAndAds', $areasTablefield );      
          }      
          return $f;
       }

    .... ect

    What I need to be able to do is filter the ManyManyComplexTableField by the BannerType field which exists in a BannerChooser...

    so it kind needs to get from Page->Banner->BannerChooser.BannerType, and then filter based on it.

    Any ideas

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