Silverstripe admin: “Has one” dropdown converts to ordinary input field after import

回眸只為那壹抹淺笑 提交于 2019-12-01 20:37:19

问题


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:

After:


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:


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.



来源:https://stackoverflow.com/questions/21235458/silverstripe-admin-has-one-dropdown-converts-to-ordinary-input-field-after-im

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!