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.

Customising the CMS /

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

CheckboxSetField set checked items from many many relationship


Go to End


1194 Views

Avatar
Vix

Community Member, 60 Posts

1 October 2015 at 9:28pm

Edited: 01/10/2015 9:29pm

I have a many_many relationship that I am trying to set with a CheckboxSetField.

In my getCMSFields function I have:

$fields->addFieldToTab("Root.Vehicle", new CheckboxSetField('Categories', 'CategoryPage', $this->getCategories(), $this->CurrentCategories()));

And the corresponding functions are:

function getCategories(){
		if($Pages = DataObject::get('CategoryPage')){
		  $data = array();
		  foreach($Pages as $page){
			  $data[$page->ID] = $page->Title;
		  }
		  return $data;
		} else {
		  return array('No Objects found');
		}
	}
	
	function CurrentCategories(){
		$data = array();
		$do = CategoryPage::get()->leftJoin('ProductPage_Categories', '"CategoryPage"."ID" = "ProductPage_Categories"."CategoryPageID"')->filter(array("ProductPageID" => $this->ID));
		if($do){
			foreach($do as $cat){
				$data[$cat->ID] = $cat->ID;
			}
			return $data;
		}
	}

The categories are saving to the database no problem, but I can't get the checkboxes to display as checked. If I echo out the CurrentCategories function results the correct information is there, just not sure what I am missing to make the boxes show as checked.

Thanks