Jump to:

3433 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Custom selection based on an existing dataobject in another template-problem

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

Page: 1
Go to End
Author Topic: 1834 Views
  • SilverRay
    Avatar
    Community Member
    167 Posts

    Custom selection based on an existing dataobject in another template-problem Link to this post

    OK, I have a dataobject called Product. I use this in a has_one relationship with ProductPage. Works well, I can make product pages and select a product etc.

    Now, I want to make another template, let's call it Custom.php, in which I want to choose a custom selection of the existing products that are showed in the normal product pages and display various selections in a bunch of custom pages, with links to the actual product pages. So I thought of setting up a many_many using the same dataobject Product, and then be able to select the products for the custom set. A while ago I had this working using a has_one, but I cannot find my code anymore, and this time I want to use a many_many because I want to be able to have more than one custom selection. How would I do that? I tried some things with getmanymanycomponents etc. but got stuck... anyone has any suggestions? Thanks!

  • dio5
    Avatar
    Community Member
    501 Posts
  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Custom selection based on an existing dataobject in another template-problem Link to this post

    Oh yeah, I did, quite a while ago already ;)

    The issue is more that I want to "switch on" products in my custom template, but those products are part of another section. Maybe I'm not clear in my question perhaps... I'll post code later today to clarify.

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Custom selection based on an existing dataobject in another template-problem Link to this post

    OK, I think I figured it out. Actually, thanks to dio5 indeed, because I revisited the relationship tutorial and it brought me back to the basics again, which is sometimes needed when you over-think things, as I tend to do ;)
    Thanks dio5! Anyway, here's some code extracts (some irrelevant details taken out for brevity).

    I have a DataObject called Product:

    class Product extends DataObject {

       public static $db = array(
          'ProductNumber' => 'Text',
          'ProductName' => 'Text',
          'ProductDescription' => 'Text'
       );

       function getCMSFields_forPopup() {
          $fields = new FieldSet();
    //code for fields etc.
          return $fields;
       }

    Then I have a ProductPage:

    class ProductPage extends Page {

       public static $db = array(
       "SomeSwitch" => "Boolean"
       );

       public static $has_one = array(
          'Image01' => 'Image',
          'MyProduct' => 'Product'
       );

       public static $many_many = array(
          'ProductColors' => 'ProductColor'
       );

       public static $belongs_many_many = array(
          'CustomPages' => 'CustomPage'
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();

          $productsTablefield = new HasOneComplexTableField(
             $this,
             'MyProduct',
             'Product',
             array(
                'ProductNumber' => 'Number',
                'ProductName' => 'Name',
                'ProductDescription' => 'Description'
             ),
             'getCMSFields_forPopup'
          );
          $productsTablefield->setParentClass('ProductPage');
          $productsTablefield->setOneToOne();
          $productsTablefield->setPageSize(50);
          $productsTablefield->setAddTitle( 'a Product' );

          $colorsTablefield = new ManyManyComplexTableField(
             $this,
             'ProductColors',
             'ProductColor',
             array(
                'ColorName' => 'Color name',
                'ColorCode' => 'Color code'
             ),
             'getCMSFields_forPopup'
          );
          $colorsTablefield->setParentClass('ProductPage');
          $colorsTablefield->setPageSize(50);
          $colorsTablefield->setAddTitle( 'a Color' );

          $fields->addFieldToTab( 'Root.Content.Images', new CheckboxField("SomeSwitch", "Test switch."));
          $fields->addFieldToTab('Root.Content.Images', new ImageField('Image01', 'Image 1.'));

          $fields->addFieldToTab( 'Root.Content.Products', $productsTablefield );
          $fields->addFieldToTab( 'Root.Content.Colors', $colorsTablefield );

          return $fields;
       }

    }

    class ProductPage_Controller extends Page_Controller {

    }

    This works of course, I can make product pages, select an associated product in the table, have it rendered in the template etc., the usual. Then I want to make a custom page with a custom selection of products that link to their respective product pages:

    class CustomPage extends Page {

       public static $db = array(
       );

       public static $has_one = array(
       );

       public static $many_many = array(
          'ProductPages' => 'ProductPage'
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $customsetTablefield = new ManyManyComplexTableField(
             $this,
             'ProductPages',
             'ProductPage',
             array(
                'MenuTitle' => 'Name'
             ),
             'getCMSFields_forPopup'
          );
          $customsetTablefield->setParentClass('CustomPage');
          $customsetTablefield->setPageSize(50);
          $customsetTablefield->setPermissions(array());

          $fields->addFieldToTab( 'Root.Content.ProductsToShow', $customsetTablefield );
          return $fields;
       }

    }

    class CustomPage_Controller extends Page_Controller {
    }

    Now I can make various custom pages, select a bunch of products per custom page, render it with a ProductPages control block including links to the original product pages, etc. That was all there is to it. First I was thinking of using all kinds of functions with DataObject::get statements etc. but there's no need. The power of an object oriented framework indeed!

    If you see anything wrong, please let me know. The only thing I'm stuck at is how to show Product properties like ProductName, ProductNumber etc. in the productsTablefield...

    Thanks!

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