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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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

Avatar
CodeGuerrilla

Community Member, 105 Posts

29 January 2010 at 6:02pm

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

Product.php

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 :)

Avatar
CodeGuerrilla

Community Member, 105 Posts

2 February 2010 at 4:21pm

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

ProductAdmin.php

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

ProductAdmin.js

// groups dropdown
	$.getJSON("admin/products/groups",
        function(data){
		  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.

Avatar
dewoob

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(
        "Channels.ID"
    );

}

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?

Avatar
CraftsMan

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'