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.

Data Model Questions /

ModelAdmin, Dropdown


Reply


7 Posts   2365 Views

Avatar
richard-ward

Community Member, 31 Posts

19 November 2009 at 9:12am

Hi,

Given the example at the following [url=http://doc.silverstripe.org/doku.php?id=modeladmin]ModelAdmin[/url], 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

Avatar
richard-ward

Community Member, 31 Posts

24 November 2009 at 12:17am

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

Avatar
Fuzz10

Community Member, 787 Posts

4 December 2009 at 10:08am

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

;-)

Avatar
richard-ward

Community Member, 31 Posts

11 December 2009 at 5:07am

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'
);
}

Avatar
Fuzz10

Community Member, 787 Posts

11 December 2009 at 10:38pm

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

Avatar
richard-ward

Community Member, 31 Posts

12 December 2009 at 7:34am

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

Avatar
Fuzz10

Community Member, 787 Posts

12 December 2009 at 8:30am

Edited: 12/12/2009 8:31am

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")));