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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

filtering Dataobject ::get for a CheckboxSetField

Go to End

3 Posts   743 Views


Community Member, 48 Posts

10 February 2012 at 3:43am

I currently have 2 Dataobjects, "Classroom" and "Students". In Classroom, I currently have a working CheckboxsetField that lets me add students to it by getting the names of the students from the Students Dataobject:

$studentlist = DataObject::get("Students");
$mapstudents = $studentlist ? $mapstudents = $studentlist->toDropdownMap('ID','Title') : array();
$fields->addFieldToTab('Root.Main', new CheckboxsetField('App','Apps', $mapstudents));

Since I have a 1:n relation established, each student can only be in 1 classroom (which is what I want). The problem is the CheckboxsetField always shows every student. If I create a new classroom record and select a student that was already in another class, that student is moved to the new record and remove from the previous class.

What I want to happen is the Checkboxset to only show students that haven't been assigned to a classroom.

Any help would be appreciated,


Forum Moderator, 1349 Posts

10 February 2012 at 1:49pm

Edited: 10/02/2012 1:50pm

If you set uo Student to have

static $has_one= array('Classroom' => 'Classroom');

You could do something like

DataObject::get('Student', 'ClassroomID' = '0' or is null 'ClassroomID');


Community Member, 48 Posts

11 February 2012 at 2:35am

Thanks, worked perfectly! I need to get in the practice of looking at the tables a little more ( phpmyadmin or DBplumber). I forgot about the classroomID column being created because of the $has_one. I didn't realize that I had something I could filter on that easily. Thanks again!!