4

I am having some problems with the admin of Silverstripe. I defined a database model (see class definitions below), and after I do a dev/build everything is looking as expected. When I try to add a new "package" all the "has one" fields are there with a drop down (see screen shot 1). I also built an importer which imports these packages. When run, everything is looking fine, except when you open a package. Then 'Festival' is correctly coupled. You can see the name, and you can select the drop down. "Troupe", on the other hand, has mysteriously converted to an input field that only shows the id of the record in the other table (See screen shot 2).

Does anyone know what is happening here? Is there something that triggers this behaviour that I am unaware off? Is there something wrong with my code (yes, but related to this problem? ;-))? I have checked the structure of the tables, and there is nothing suspicious there...

Before: before import

After: after import


Package.php

    class Package extends DataObject {
        public static $db = array(
            'Number'                    => 'Int',
            'Title'                     => 'Varchar(255)',
            'Description'               => 'HTMLText',
            'Credits'                   => 'HTMLText',
        );

        public static $has_many = array(
            'Events'    => 'Event',
        );

        public static $many_many = array(
           'Genres'            => 'Genre',
        );

        public static $has_one = array(
            'Festival'          => 'Festival',
            'Troupe'            => 'Troupe',
        );
    }

    class PackageAdmin extends ModelAdmin {
        public static $managed_models       = array('Package'); // Can manage multiple models
        static $url_segment                 = 'packages'; // Linked as /admin/packages/
        static $menu_title                  = 'Packages';
    }

Troupe.php

    class Troupe extends DataObject {
        public static $db = array(
            "Name"          => "Varchar(255)",
            "Description"   => "HTMLText",
            "Url"           => "Varchar(255)",
        );

        public static $has_many = array(
            'Packages'      => 'Package.Troupe',
        );
    }

    class TroupeAdmin extends ModelAdmin {
        public static $managed_models       = array('Troupe','Package'); // Can manage multiple models
        static $url_segment                 = 'troupes'; // Linked as /admin/troupes/
        static $menu_title                  = 'Troupes';
    }

Festival.php

class Festival extends DataObject {

    public static $db = array(
        'Name'          => 'Varchar(255)',
        'Description'   => 'HTMLText'
    );

    public static $has_many = array(
        'Packages' => 'Package.Festival'
    );
}

class FestivalAdmin extends ModelAdmin {
    public static $managed_models       = array('Festival','Package'); // Can manage multiple models
    static $url_segment                 = 'festivals'; // Linked as /admin/festivals/
    static $menu_title                  = 'Festivals';
}

1 Answer 1

6

You probably shouldn't only rely on the admin scaffolding, and use getCMSFields on your DataObjects to customize what happen in the CMS. In you case, a simple replace of the Troupe dropdown could work, adding this to your Package class:

function getCMSFields()
{
    $fields = parent::getCMSFields();

    $troupeList = Troupe::get()->map()->toArray();
    $troupeSelect = DropdownField::create('TroupeID', 'Troupe')->setSource($troupeList);

    $fields->replaceField('TroupeID', $troupeSelect);

    return $fields;
}

This is quite minimalist and a lot more could me customised.

5
  • i agree, anyway it would be interesting why scaffolding doesn't work right here.
    – schellmax
    Commented Jan 21, 2014 at 7:58
  • Thanks! This did the trick. However it is not so much a solution as a workaround, as Schellmax already implied. But for me it is perfect, as I don't have the time to research the problem thoroughly.
    – jberculo
    Commented Jan 21, 2014 at 8:53
  • 7
    in the ForeignKey class, a dropdown is generated for ($list->count() < 100) and some comments "Don't scaffold a dropdown for large tables...might exceed the available PHP memory"...
    – colymba
    Commented Jan 21, 2014 at 11:26
  • I was suspecting something like that, and it makes sense. At present I do not have performance issues, so I'll leave them in...
    – jberculo
    Commented Jan 21, 2014 at 11:28
  • @colymba +1 for further investing into the topic. good to know
    – schellmax
    Commented Jan 21, 2014 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.