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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

All other Modules /

Discuss all other Modules here.

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

ModelAdmin $searchable_fields $has_one as dropdown

Go to End

4 Posts   4086 Views


Community Member, 105 Posts

29 January 2010 at 6:02pm

Suppose we are following this tutorial on ModelAdmin and we changed the following to:


static $searchable_fields = array(
		'Name' => array('title'=>'Name'),
		'ProductCode' => array('title'=>'Product Code'),
		'Category.Name' => array('title'=>'Product Group') // add category name to searchable fields

Great we can search the category names with a text field, seeing as this is a $has_one relationship wouldn't it make sense to have a dropdown instead so you can simply select a Category from the list?

With that being said it really doesn't look like ModelAdmin supports this without overriding ModelAdmin's SearchForm method is that correct?

Has anyone done this if please share, or if there an easier way please tell :)


Community Member, 105 Posts

2 February 2010 at 4:21pm

Okay another nasty jquery hack but way faster than overloading/subclassing ModelAdmin:


class ProductAdmin extends ModelAdmin {
    function groups()
		$groups = DataObject::get('ProductGroup');
		$out = array();
		foreach($groups as $group) {
			array_push($out, $group->toMap());
		return json_encode($out);


// groups dropdown
		  var Options = '<option></option>';
		  $.each(data, function(){
			Options = Options + '<option value="'+this.ProductGroup+'">'+this.ProductGroup+'</option>';
		  $('#Form_SearchForm_Product_ProductGroups__ProductGroup').replaceWith('<select name="ProductGroups__ProductGroup" id="Form_SearchForm_Product_ProductGroups__ProductGroup" class="text">'+Options+'</select>');

Just remember the above dropdown needs the correct ID and NAME attributes from your ModelAdmin generated search forms.


Community Member, 10 Posts

26 May 2010 at 2:00am

Thank you very much, this helped me too.

Btw, before searching through the forums, I tried to just add the ID of a many_many associated class to the $searchable_fields variable, e.g.:

class Foo extends DataObject {

$many_many = array(
   "Channels" => "ChannelPage"

static $searchable_fields = array(


Using this code, a dropdown box with valid options was displayed in ModelAdmin. Hurray! Seems like it's designed to work that way.

Unfortunately, an error occured after clicking on the search button: The generated SQL was invalid ("Unknown column 'ChannelPage.ID' in 'where clause').

IMHO, it seems like there's a bug in the SQL generation. The searchable_fields are not translated to a where clause correctly. Actually, the "Channels" role should be translated to "Foo_Channels" instead of "ChannelPage", and the ID property should be translated to "ChannelPageID" column. So the column identifier in the where clause should read 'Foo_Channels.ChannelPageID' instead of 'ChannelPage.ID'.

Can anyone confirm that?


Community Member, 35 Posts

4 February 2011 at 1:45am

I am using version 2.4.3 and

	static $searchable_fields = array(
		'ArticleTypeID' =>array( 'title' => 'Article Type' )

seems to create a drop down as expected but the first option is a blank option. I can set a default value to pre-select and option. I am trying to remove the first blank entry or change the text to say 'Please select'