mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
FIX: Searchable fields with dot notation can be inherited from summary_fields (fixes #1429)
This commit is contained in:
parent
0f1ae75dd8
commit
a91a4bbdc2
@ -3179,16 +3179,25 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
$fields = $this->stat('searchable_fields');
|
||||
$labels = $this->fieldLabels();
|
||||
|
||||
// fallback to summary fields
|
||||
if(!$fields) {
|
||||
// fallback to summary fields (unless empty array is explicitly specified)
|
||||
if( ! $fields && ! is_array($fields)) {
|
||||
$summaryFields = array_keys($this->summaryFields());
|
||||
$fields = array();
|
||||
|
||||
// remove the custom getters as the search should not include.
|
||||
// remove the custom getters as the search should not include them
|
||||
if($summaryFields) {
|
||||
foreach($summaryFields as $key => $name) {
|
||||
if($this->hasDatabaseField($name) || $this->relObject($name)) {
|
||||
$spec = $name;
|
||||
|
||||
// Extract field name in case this is a method called on a field (e.g. "Date.Nice")
|
||||
if(($fieldPos = strpos($name, '.')) !== false) {
|
||||
$name = substr($name, 0, $fieldPos);
|
||||
}
|
||||
|
||||
if($this->hasDatabaseField($name)) {
|
||||
$fields[] = $name;
|
||||
} elseif($this->relObject($spec)) {
|
||||
$fields[] = $spec;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -646,6 +646,43 @@ class DataObjectTest extends SapphireTest {
|
||||
);
|
||||
}
|
||||
|
||||
public function testSearchableFields() {
|
||||
$player = $this->objFromFixture('DataObjectTest_Player', 'captain1');
|
||||
$fields = $player->searchableFields();
|
||||
$this->assertArrayHasKey(
|
||||
'IsRetired',
|
||||
$fields,
|
||||
'Fields defined by $searchable_fields static are correctly detected'
|
||||
);
|
||||
$this->assertArrayHasKey(
|
||||
'ShirtNumber',
|
||||
$fields,
|
||||
'Fields defined by $searchable_fields static are correctly detected'
|
||||
);
|
||||
|
||||
$team = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
||||
$fields = $team->searchableFields();
|
||||
$this->assertArrayHasKey(
|
||||
'Title',
|
||||
$fields,
|
||||
'Fields can be inherited from the $summary_fields static, including methods called on fields'
|
||||
);
|
||||
$this->assertArrayHasKey(
|
||||
'Captain.ShirtNumber',
|
||||
$fields,
|
||||
'Fields on related objects can be inherited from the $summary_fields static'
|
||||
);
|
||||
$this->assertArrayHasKey(
|
||||
'Captain.FavouriteTeam.Title',
|
||||
$fields,
|
||||
'Fields on related objects can be inherited from the $summary_fields static'
|
||||
);
|
||||
|
||||
$testObj = new DataObjectTest_Fixture();
|
||||
$fields = $testObj->searchableFields();
|
||||
$this->assertEmpty($fields);
|
||||
}
|
||||
|
||||
public function testDataObjectUpdate() {
|
||||
/* update() calls can use the dot syntax to reference has_one relations and other methods that return
|
||||
* objects */
|
||||
@ -1191,6 +1228,11 @@ class DataObjectTest_Player extends Member implements TestOnly {
|
||||
private static $belongs_many_many = array(
|
||||
'Teams' => 'DataObjectTest_Team'
|
||||
);
|
||||
|
||||
private static $searchable_fields = array(
|
||||
'IsRetired',
|
||||
'ShirtNumber'
|
||||
);
|
||||
}
|
||||
|
||||
class DataObjectTest_Team extends DataObject implements TestOnly {
|
||||
@ -1220,6 +1262,12 @@ class DataObjectTest_Team extends DataObject implements TestOnly {
|
||||
)
|
||||
);
|
||||
|
||||
private static $summary_fields = array(
|
||||
'Title.UpperCase' => 'Title',
|
||||
'Captain.ShirtNumber' => 'Captain\'s shirt number',
|
||||
'Captain.FavouriteTeam.Title' => 'Captain\'s favourite team'
|
||||
);
|
||||
|
||||
private static $default_sort = '"Title"';
|
||||
|
||||
public function MyTitle() {
|
||||
@ -1251,6 +1299,13 @@ class DataObjectTest_Fixture extends DataObject implements TestOnly {
|
||||
'MyFieldWithDefault' => 'Default Value',
|
||||
);
|
||||
|
||||
private static $summary_fields = array(
|
||||
'Data' => 'Data',
|
||||
'DateField.Nice' => 'Date'
|
||||
);
|
||||
|
||||
private static $searchable_fields = array();
|
||||
|
||||
public function populateDefaults() {
|
||||
parent::populateDefaults();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user