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 /

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

CheckboxSetField and many_many relation

Go to End

2 Posts   3004 Views


Community Member, 2 Posts

24 March 2010 at 12:28pm

Edited: 24/03/2010 12:30pm

Hi guys,

i can't figure out how to use CheckboxSetField for a many_many relation.

My classes are looking like this:

class Team extends DataObject {
   static $db = array (
      'Title' => 'Varchar(255)',
   static $belongs_many_many = array (
      'Coworkers' => 'Coworker'

class Coworker extends Member {
   static $has_one = array(
      'StudentCommunity' => 'StudentCommunity',
   static $many_many = array (
      'Teams' => 'Team'
   public function getFrontendFields() {
      $fields = $this->scaffoldFormFields(
            'restrictFields' => array(
            'fieldClasses' => array(
               'Email' => 'EmailField',
               'Password' => 'ConfirmedPasswordField',
      $teamList = DataObject::get('Team');
         new CheckboxSetField('Teams', '', $teamList)
      return $fields;

But it doesn't matter what i'm doing. when inserting a new "Coworker" via the frontend Silverstripe is not adding any rows into the Coworker_Teams table.

Does anyone sees what i'm missing?



Forum Moderator, 1891 Posts

1 April 2010 at 4:16am


I use ModelAdmin and overload getCMSFields (use the result of the parent class)... I would do what you are asking like this...

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

		$doTeam = DataObject::get("Team");
		$mapTeam = $doTeam ? $mapTeam = $doTeam->toDropdownMap('ID','Title') : array();
		$fields->push(new CheckboxSetField('Teams','Teams', $mapTeam));

		return $fields;

..hope it helps