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.

Data Model Questions /

$many_many - tracking down the ole relation chain


Reply


908 Views

Avatar
NickJacobs

Community Member, 144 Posts

15 February 2010 at 9:58pm

Edited: 15/02/2010 10:06pm

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 :)