Skip to content

Instantly share code, notes, and snippets.

@wilr
Created December 7, 2021 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilr/8aa124a8b40500d7ad8a2bf6659865f1 to your computer and use it in GitHub Desktop.
Save wilr/8aa124a8b40500d7ad8a2bf6659865f1 to your computer and use it in GitHub Desktop.
Example of how to do searchable_fields with a many_many relation
<?php
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\DropdownField;
use SilverStripe\ORM\Filters\ExactMatchFilter;
class Project extends DataObject
{
private static $db = [
'Title' => 'Varchar(200)',
];
private static $many_many = [
'Regions' => Region::class,
];
private static $summary_fields = [
'ID' => 'ID',
'Title' => 'Name'
];
private static $searchable_fields = [
'Title',
'Regions.ID'
];
private static $default_sort = 'Title ASC';
public function searchableFields()
{
$fields = parent::searchableFields();
$fields['Regions.ID'] = [
'filter' => ExactMatchFilter::class,
'title' => 'Region',
'field' => DropdownField::create('Region')->setSource(
Region::get()
)->setEmptyString('All regions')
];
return $fields;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment