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.

Archive /

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

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

tutorial #5 question: how to make has_many dropdown

Go to End

2 Posts   3618 Views


Community Member, 4 Posts

24 May 2008 at 3:01am

Edited: 24/05/2008 3:01am

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)
                    = $Nationality->getField('Nationality');

            new DropdownField_withAdd(

        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;



Community Member, 712 Posts

24 May 2008 at 1:00pm

Had a quick look at the code. The documentation isn't very helpful on this one, but the important bit is this:

// from Line 123 of sapphire/forms/DropdownField.php

if($this->addLink) $addLink = <<<HTML
<a class="addlink link" id="{$this->name}_addLink" href="$this->addLink" style="display: inline; padding-left: 1em; text-decoration: underline;">$this->addText</a>

So, there should be a link next to your dropdown box but buggered if I can get it to show up here. Also, as far as I can see, even if you get the link to show you have to create a controller that actually does the adding. There is nothing actually in the class that does this for you, and it's a bit beyond me at the moment.

A small thing, I think you also need to add a $has_many into your Nationalities object to complete the relationship properly.

static $has_many = array ( 'Students' => 'Student' );

Hope this helps.