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.

Data Model Questions /

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

CheckboxSetField and many_many relation


Go to End


2 Posts   3767 Views

Avatar
jens

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(
         array(
            'restrictFields' => array(
               'FirstName',
               'Surname',
               'Email',
               'Password',
               'StudentCommunity',
            ),
            'fieldClasses' => array(
               'Email' => 'EmailField',
               'Password' => 'ConfirmedPasswordField',
            )
         )
      );
      $teamList = DataObject::get('Team');
      $fields->push(
         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?

Thx
Jens

Avatar
swaiba

Forum Moderator, 1899 Posts

1 April 2010 at 4:16am

Hi,

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();

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

		return $fields;
	}

..hope it helps