Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make dropdownFieldThreshold configurable on DBForeignKey #7789

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ en:
other: '{count} years'
SilverStripe\ORM\FieldType\DBEnum:
ANY: Any
SilverStripe\ORM\FieldType\DBForeignKey:
DROPDOWN_THRESHOLD_FALLBACK_MESSAGE: 'Too many related objects; fallback field in use'
SilverStripe\ORM\Hierarchy:
LIMITED_TITLE: 'Too many children ({count})'
SilverStripe\ORM\Hierarchy\Hierarchy:
Expand Down
14 changes: 13 additions & 1 deletion src/ORM/FieldType/DBForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class DBForeignKey extends DBInt
*/
protected $object;

/**
* This represents the number of related objects to show in a dropdown before it reverts
* to a NumericField. If you are tweaking this value, you should also consider constructing
* your form field manually rather than allowing it to be scaffolded
*
* @config
* @var int
*/
private static $dropdown_field_threshold = 100;

private static $index = true;

private static $default_search_filter_class = 'ExactMatchFilter';
Expand Down Expand Up @@ -63,11 +73,13 @@ public function scaffoldFormField($title = null, $params = null)
$list = DataList::create($hasOneClass);
// Don't scaffold a dropdown for large tables, as making the list concrete
// might exceed the available PHP memory in creating too many DataObject instances
if ($list->count() < 100) {
$threshold = self::config()->get('dropdown_field_threshold');
if ($list->count() < $threshold) {
$field = new DropdownField($this->name, $title, $list->map('ID', $titleField));
$field->setEmptyString(' ');
} else {
$field = new NumericField($this->name, $title);
$field->setRightTitle(_t(self::class . '.DROPDOWN_THRESHOLD_FALLBACK_MESSAGE', 'Too many related objects; fallback field in use'));
}
return $field;
}
Expand Down