Jump to:

3462 Posts in 1065 Topics by 740 members

Data Model Questions

SilverStripe Forums » Data Model Questions » CheckboxSetField and many_many relation

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

Page: 1
Go to End
Author Topic: 2149 Views
  • jens
    Community Member
    2 Posts

    CheckboxSetField and many_many relation Link to this post

    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?


  • swaiba
    Forum Moderator
    1796 Posts

    Re: CheckboxSetField and many_many relation Link to this post


    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

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.