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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

HasMany CheckBox Disabled


Go to End


2 Posts   1824 Views

Avatar
eceers

Community Member, 24 Posts

21 April 2010 at 6:37pm

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.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

22 April 2010 at 1:49am

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