Created
December 7, 2021 21:23
-
-
Save wilr/8aa124a8b40500d7ad8a2bf6659865f1 to your computer and use it in GitHub Desktop.
Example of how to do searchable_fields with a many_many relation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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