Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » how to make scaffolding show has_many as dropdown?

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 1518 Views
  • liveyoungmedia
    Avatar
    Community Member
    4 Posts

    how to make scaffolding show has_many as dropdown? Link to this post

    I was going through tutorial #5, and decided that I would like to see if the admin panel scaffolding could easily show a dropdown of choices when presented with a has_many datamodel relationship.

    So I decided to make Student has_many Nationality, and promote nationality from a simple text field to a database object. Then I found a class called "DropdownField_withAdd"... and got so far as to having the "Add Student" popup link in the CMS display a dropdown called "Nationality"... but of course the dropdown is empty, and I don't see a way to get it to give me the option of creating new nationalities....

    Here's the relevant code I created:

    class Student extends DataObject {

    static $db = array(
    'FirstName' => 'Text',
    'LastName' => 'Text'
    );

    static $has_one = array(
    'Mentor' => 'Mentor',
    'Nationality' => 'Nationality'
    );

    public function getCMSFields_forPopup(){
    $fields = new FieldSet();
    $fields->push(new TextField('FirstName', 'First Name'));
    $fields->push(new TextField('LastName', 'Last Name'));

    $Nationalities = Nationality::get('Nationality');

    $Nationalities_array = array();
    if ($Nationalities)
    foreach ($Nationalities as $Nationality)
    $Nationalities_array[$Nationality->getField('ID')]
    = $Nationality->getField('Nationality');

    $fields->push(
    new DropdownField_withAdd(
    'Nationality',
    'Nationality',
    $Nationalities_array,
    'addLink'
    )
    );

    return $fields;
    }

    }

    -------------------------------------

    class Nationality extends DataObject {

    static $db = array(
    'Nationality' => 'Text'
    );

    public function getCMSFields_forPopup(){
    $fields = new FieldSet();
    $fields->push(new TextField('Nationality', 'Nationality'));
    return $fields;
    }

    }

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