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.

General Questions /

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

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

SS3 - how to set searchable fields for a dataobject


Go to End


13 Posts   11739 Views

Avatar
BenWu

Community Member, 97 Posts

27 July 2012 at 1:59am

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

Community Member, 25 Posts

27 July 2012 at 2:04am

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

Community Member, 1 Post

15 February 2013 at 6:05am

Edited: 15/02/2013 6:06am

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
JonShutt

Community Member, 244 Posts

16 June 2013 at 10:46am

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

Avatar
JonShutt

Community Member, 244 Posts

4 July 2013 at 11:05am

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

Go to Top