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, biapar, Willr, Ingo, swaiba

CheckboxSetField and many_many relation


Go to End
Reply


2 Posts   2367 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, 1823 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