Jump to:

10931 Posts in 2614 Topics by 1810 members

All other Modules

SilverStripe Forums » All other Modules » ModelAdmin $searchable_fields $has_one as dropdown

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 2789 Views
  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    ModelAdmin $searchable_fields $has_one as dropdown Link to this post

    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

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: ModelAdmin $searchable_fields $has_one as dropdown Link to this post

    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.

  • dewoob
    Avatar
    Community Member
    10 Posts

    Re: ModelAdmin $searchable_fields $has_one as dropdown Link to this post

    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?

  • CraftsMan
    Avatar
    Community Member
    35 Posts

    Re: ModelAdmin $searchable_fields $has_one as dropdown Link to this post

    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'

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