Jump to:

23451 Posts in 18899 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » filtering Dataobject ::get for a CheckboxSetField

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

Page: 1
Go to End
Author Topic: 408 Views
  • woodb2
    Avatar
    Community Member
    48 Posts

    filtering Dataobject ::get for a CheckboxSetField Link to this post

    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

  • martimiz
    Avatar
    Forum Moderator
    1078 Posts

    Re: filtering Dataobject ::get for a CheckboxSetField Link to this post

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

  • woodb2
    Avatar
    Community Member
    48 Posts

    Re: filtering Dataobject ::get for a CheckboxSetField Link to this post

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

    408 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.