Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » HasMany CheckBox Disabled

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1333 Views
  • eceers
    Avatar
    Community Member
    24 Posts

    HasMany CheckBox Disabled Link to this post

    Hi all,

    I have created a ProductPage Template that I want to be able to use in a site.

    Code example below;

       class ProductPage extends Page {
       
          public static $db = array(
             'Price' => 'Currency',
             'SalePrice' => 'Currency',
             'ProductCode' => 'Text'
          );

          public static $has_many = array(
             'ProductImages' => 'ProductImage'
          );
          public static $has_one = array(
             'ProductPage' => 'Homepage'
             );

          function getCMSFields() {
             $fields = parent::getCMSFields();
       
             $fields->addFieldToTab('Root.Content.Main', new TextField('Price', 'Price'), 'Content');
             $fields->addFieldToTab('Root.Content.Main', new TextField('SalePrice', 'SalePrice'), 'Content');
             $fields->addFieldToTab('Root.Content.Main', new TextField('ProductCode', 'ProductCode'), 'Content');

             $tablefield = new DataObjectManager(
                         $this,
                         'ProductImages',
                         'ProductImage',
                         array( 'Thumbnail' => 'Image', 'Title' => 'Title'),
                         'getCMSFields_forPopup'
                         );

             $tablefield->setPageSize(100);
             $tablefield->setAddTitle( 'A Image' );

             $fields->addFieldToTab( 'Root.Content.ProductImages', $tablefield );

             return $fields;
          }
       
       }

    Now I would like to gather all these products and give the user the oppurtunity to display them on the homepage in a list. I had a thought, I can use HasManyDataObjectManager. So I have written the HomePage like this;

       class HomePage extends Page {
       
          public static $db = array(
          );
       
          public static $has_one = array(
          );
          public static $has_many = array(
             'Specials' => 'ProductPage'
          );
       
          function getCMSFields() {
             $fields = parent::getCMSFields();

             $manager = new HasManyDataObjectManager(
                         $this,
                         'Specials',
                         'ProductPage',
                         array('Title' => 'Title'),
                         'getCMSFields_forPopup'
                         );
             $perms = array();//Don't want to be able to Remove, Delete, View, Add from here.
             $manager->setPermissions($perms);

             $fields->addFieldToTab( 'Root.Content.Specials', $manager );

             return $fields;
          }
       }

    Now it all works sweet with one small exception, I can't select the item in the admin. I can see the checkbox but it is disabled.

    The problem doesn't appear to be DataObjectManager Specific as I removed it and went back to the standard method with the same result.

    Any help would be greatly appreciated.

  • UncleCheese
    Avatar
    4085 Posts

    Re: HasMany CheckBox Disabled Link to this post

    Because you're using a HasManyDOM, remember that the reciprocal of a has_many is a has_one, which means that if those records are already related to a different object, they're not going to be selectable. You may want ManyManyDOM, or, if you're not doing any add/delete/edit, use a CheckboxSetField or, my favorite, MultiSelectField (separate module).

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