Jump to:

22997 Posts in 11853 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » SS3 - how to set searchable fields for a dataobject

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2
Go to End
Author Topic: 2740 Views
  • BenWu
    Avatar
    Community Member
    87 Posts

    Re: SS3 - how to set searchable fields for a dataobject Link to this post

    many thanks!

    ModelAdmin automatically creates the Contact GridField for the LeadsAdmin. Your codes overwrite that gridfield.

    I think it is a 'bug' that GridFieldAutocompleter doesn't look into the $searchable_fields of the Contact object to find out what searchable_fields are. It only checks if the Contact object has a 'Name' field or not to decide whether the Contact object is searchable or not.

    Although your solution works but i think the perfect solution is to update The GridFieldAutoCompleter. Hope the SS team can look into this?

    regards,
    Ben

  • priithansen
    Avatar
    Community Member
    25 Posts

    Re: SS3 - how to set searchable fields for a dataobject Link to this post

    Yep at the moment it really isn't obvious what went wrong when the "No searchable fields could be found for class ##" error occurs.

  • PmKoetsier
    Avatar
    Community Member
    1 Post

    Re: SS3 - how to set searchable fields for a dataobject Link to this post

    I stumbled over the same bug but after some trial and error I decided to modify the GridFieldAutocompleter in the core. For anyone interested:
    framework/forms/gridfield/GridFieldAddExistingAutocompleter.php line 260

    Old code:

       protected function scaffoldSearchFields($dataClass) {
          $obj = singleton($dataClass);
          if($obj->hasDatabaseField('Title')) {
             return array('Title');
          } else if($obj->hasDatabaseField('Name')) {
             return array('Name');
          } else {
             return null;
          }
       }

    New code:

       protected function scaffoldSearchFields($dataClass) {
          $obj = singleton($dataClass);
          foreach($obj->searchableFields() as $k => $v){
             $searchable[] = $k;
          }
          return $searchable;
       }

  • Futureweb
    Avatar
    Community Member
    232 Posts

    Re: SS3 - how to set searchable fields for a dataobject Link to this post

    Thanks for the fix PmKoetsier,
    would be great to submit this change to the core files...

  • Futureweb
    Avatar
    Community Member
    232 Posts

    Re: SS3 - how to set searchable fields for a dataobject Link to this post

    I just created an updated function that fixed the search_feilds bug, and also sets the results format too - or else all the search results end just just as an ID

    protected function scaffoldSearchFields($dataClass) {
          $obj = singleton($dataClass);
          if($obj->hasDatabaseField('Title')) {
             return array('Title');
          } else if($obj->hasDatabaseField('Name')) {
             return array('Name');
          } else if($obj::$searchable_fields){
    $this->resultsFormat = "$".implode(", $", $obj::$searchable_fields);
    return $obj::$searchable_fields;
    } else {
    return null;
    }
    }

    2740 Views
Page: 1 2
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.