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.

General Questions

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

SS3 - how to set searchable fields for a dataobject


Reply

13 Posts   3428 Views

Avatar
BenWu

27 July 2012 at 1:59am Community Member, 90 Posts

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

Avatar
priithansen

27 July 2012 at 2:04am Community Member, 25 Posts

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

Avatar
PmKoetsier

15 February 2013 at 6:05am (Last edited: 15 February 2013 6:06am), Community Member, 1 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;
   }

Avatar
Futureweb

16 June 2013 at 10:46am Community Member, 233 Posts

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

Avatar
Futureweb

4 July 2013 at 11:05am Community Member, 233 Posts

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