Jump to:

3433 Posts in 1058 Topics by 734 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: 2114 Views
  • jens
    Avatar
    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(
    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

  • swaiba
    Avatar
    Forum Moderator
    1785 Posts

    Re: CheckboxSetField and many_many relation Link to this post

    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

    2114 Views
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.