Fix a crash related to $searchable_fields

Silverstripe Version: 4.3

Hi, I know $searchable_fields is normally based on the content of $summary_fields in cases when $searchable_fields is not defined on a DataObject class. I think most of the time I don’t define $searchable_fields, but I quite often do define $summary_fields.

The problem is that sometimes I tend to include some non database “field” names in $summary_fields (custom method names rather than fields) which will lead to those non-db-fields being used for the $searchable_fields array as well if $searchable_fields is not explicitly defined. This leads to SilverStripe crashing when it tries to scaffolder a search form: PHP Fatal error: Call to a member function scaffoldSearchField() on string in vendor/silverstripe/framework/src/ORM/DataObject.php on line 2255. Perhaps the error can differ from time to time, but the cure is always the same: remember to define $searchable_fields in your DataObject class and make it only contain database fields to be safe and sound. For me it’s easy to find out that I need to define $searchable_fields when I have defined non-database fields in $summary_fields, but I guess this must be quite hard for newbies to find out.

I’ve ran into this situation multiple times over the years and finally thought that maybe this is something that could be actually fixed in a permanent way. I haven’t looked at the actual code that borrows fields from $summary_fields when $searchable_fields is not defined, but I think whatever the mechanism is, we could add some kind of filter into it that would iterate $summary_fields and only borrow those fields that actually do exist in the database.

Any thoughts on this? :slight_smile: I think I can dig deeper into this to create a pull request if I know that there will be interest for this. :slight_smile:


use SilverStripe\ORM\DataObject;

class MyObject extends DataObject
	private static $db = [
		'MyFieldName' => 'Text',
	private static $summary_fields = [
	// Leave $searchable_fields undefined to make it actually have the same content as $summary_fields:
	// private static $searchable_fields = [
	// 	'MyFieldName', // This is OK since it's a database fields
	// 	'HelloWorld', // This will make SilverStripe crash
	// ];
	public function HelloWorld()
		return 'Hi!';

An old issue about this in GitHub

Current issue is BadMethodCallException when scafolding search · Issue #8792 · silverstripe/silverstripe-framework · GitHub

It seems this bug was introduced in SS4.3 and drives many devs crazy. I stumbled over this error 3 or 4 times today and helped some devs on slack fixing it.

Yeah, it seems I created this topic unnecessarily. Well, perhaps this topic helps people to find the GitHub issue you just linked. So it’s better to continue the conversation in GitHub. If the forum moderators want to lock this topic, it’s okay for me :).