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

filtering Dataobject ::get for a CheckboxSetField


Reply

3 Posts   426 Views

Avatar
woodb2

10 February 2012 at 3:43am Community Member, 48 Posts

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,
Brian

Avatar
martimiz

10 February 2012 at 1:49pm (Last edited: 10 February 2012 1:50pm), Forum Moderator, 1091 Posts

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

Avatar
woodb2

11 February 2012 at 2:35am Community Member, 48 Posts

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!!