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   1846 Views

Avatar
richard-ward

19 November 2009 at 9:12am Community Member, 31 Posts

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

24 November 2009 at 12:17am Community Member, 31 Posts

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

Avatar
Fuzz10

4 December 2009 at 10:08am Community Member, 787 Posts

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

11 December 2009 at 5:07am Community Member, 31 Posts

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

11 December 2009 at 10:38pm Community Member, 787 Posts

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

12 December 2009 at 7:34am Community Member, 31 Posts

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

12 December 2009 at 8:30am (Last edited: 12 December 2009 8:31am), Community Member, 787 Posts

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