Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » ModelAdmin, Dropdown

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

Page: 1
Go to End
Author Topic: 1719 Views
  • richard-ward
    Avatar
    Community Member
    31 Posts

    ModelAdmin, Dropdown Link to this post

    Hi,

    Given the example at the following ModelAdmin, then add another field, e.g. description, into the Category class. How do I then choose which of the fields (title or description) to show in the Dropdown (as displayed at point 3 in Usage on the linked page)?

    Further to that, is it possible to concatenate them? In my application i have 3 fields, Title, Forename and Surname which I would like to concatenate into the dropdown on my page - currently it just shows Title, which isn't too helpful!!

    Regards

  • richard-ward
    Avatar
    Community Member
    31 Posts

    Re: ModelAdmin, Dropdown Link to this post

    Just bumping this - has no-one else struggled with this? I was thinking it was something other people had wanted to do!

  • Fuzz10
    Avatar
    Community Member
    786 Posts

    Re: ModelAdmin, Dropdown Link to this post

    Good one !

    I've bumped into this as well , searched a bit through the docs but in the end resorted to deleting the field with
    removeByName() and then adding it again by creating the DropDownField by hand ...

    There's probably an easier way (maybe some custom param or method on the dataobject to set the default "label") but I cannot find it..

    ;-)

  • richard-ward
    Avatar
    Community Member
    31 Posts

    Re: ModelAdmin, Dropdown Link to this post

    Thanks Fuzz10,

    I've had a look at how to remove fields and add them again, but I am struggling to get it working. I wonder if you have an example I could see?

    Assuming I have the code below, and I would like to achieve having dropdowns which show "$title - $composer" (as currently i have pieces called the same thing by different people!); Would i have to put the getCMSFields() call in Service.php and remove each of the fields relating to Repertoire so they can be re-added as dropdowns - or have one entry in Repertoire.php? Also, I don't really understand what I am supposed to add the field to as it appears that I have to attach it to a tab? Sorry - I am probably getting it all wrong!

    Repertoire.php

    class Repertoire {
    static $db = new array (
    'title' = > 'varchar',
    'composer' => 'varchar'
    );
    }

    Service.php

    class Service {
    static $db = new array (
    'type' = > 'enum('Matins, Evensong', 'Evensong')',
    'location' => 'varchar'
    );

    static $has_one = new array(
    'Introit' => 'Repertoire',
    'Anthem' => 'Repertoire',
    'Setting' => 'Repertoire'
    );
    }

  • Fuzz10
    Avatar
    Community Member
    786 Posts

    Re: ModelAdmin, Dropdown Link to this post

    I suppose the code below is just an example , but if not : you need to extend DataObject ... ;-)

    Untested code of course :

    function getCMSFields() {

    $fields = parent::getCMSFields();

    $fields->push(new DropdownField('IntroitID','Repertoire',Dataobject::get("Repertoire")->toDropdownMap("ID", "title")));

    return $fields;

    }

    Change title to your own combination of composer+title .....

  • richard-ward
    Avatar
    Community Member
    31 Posts

    Re: ModelAdmin, Dropdown Link to this post

    Thanks again for the reply (and yes, it was an example on which i forgot to put the 'extends DataObject'),

    Unfortunately I cannot work out the very last step:

    Change title to your own combination of composer+title .....

    All is well if the value is 'Title', or 'Composer', but if it is a mix, ie 'Title - Composer', then the field is empty. I can understand that as I assume it uses it as a key to get the value. I have looked up about the toDropdownMap and it can take a field or a method to put the value in. Unfortunately the method seems to need to be part of the Service object rather than Repertoire, so i dont know how i can get that to return Title - Composer.

    Stuck again!!

    Hope more help is on its way!

    Regards

  • Fuzz10
    Avatar
    Community Member
    786 Posts

    Re: ModelAdmin, Dropdown Link to this post

    Yeah sorry, I was a bit incomplete there..

    You can add a method to your dataobject class to create the value you want , and then call it in your dropdownField...

    So:

    e.g.

    function getTitleComposer(){
          return $this->Title.",".$this->Composer;
       }

    and then :

    new DropdownField('IntroitID','Repertoire',Dataobject::get("Repertoire")->toDropdownMap("ID", "getTitleComposer")));

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