Skip to content

Commit

Permalink
make dropdownFieldThreshold configurable on DBForeignKey
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewandante committed Jan 26, 2018
1 parent 0e905b9 commit 578ee5d
Showing 1 changed file with 13 additions and 1 deletion.
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('Too many related objects; fallback field in use');
}
return $field;
}
Expand Down

0 comments on commit 578ee5d

Please sign in to comment.