ENH Use SearchableDropdownField for autoscaffolded has_one relationships

This commit is contained in:
Steve Boyd 2023-12-19 10:04:01 +13:00
parent 3acd6d3147
commit e66c1aec06
4 changed files with 15 additions and 56 deletions

View File

@ -15,8 +15,8 @@ class SearchableDropdownField extends DropdownField
public function __construct( public function __construct(
string $name, string $name,
string $title, ?string $title = null,
DataList $source, ?DataList $source = null,
mixed $value = null, mixed $value = null,
string $labelField = 'Title' string $labelField = 'Title'
) { ) {

View File

@ -47,7 +47,7 @@ trait SearchableDropdownTrait
private bool $useSearchContext = false; private bool $useSearchContext = false;
private DataList $sourceList; private ?DataList $sourceList = null;
private string $labelField = 'Title'; private string $labelField = 'Title';
@ -386,7 +386,7 @@ trait SearchableDropdownTrait
/** @var DataObject $record */ /** @var DataObject $record */
$classNameField = substr($name, 0, -2) . 'Class'; $classNameField = substr($name, 0, -2) . 'Class';
if ($record->hasField($classNameField)) { if ($record->hasField($classNameField)) {
$record->$classNameField = $ids ? $this->sourceList->dataClass() : ''; $record->$classNameField = $ids ? $record->ClassName : '';
} }
} }
$record->write(); $record->write();

View File

@ -15,8 +15,8 @@ class SearchableMultiDropdownField extends MultiSelectField
public function __construct( public function __construct(
string $name, string $name,
string $title, ?string $title = null,
DataList $source, ?DataList $source = null,
$value = null, $value = null,
$labelField = 'Title' $labelField = 'Title'
) { ) {

View File

@ -5,9 +5,8 @@ namespace SilverStripe\ORM\FieldType;
use SilverStripe\Assets\File; use SilverStripe\Assets\File;
use SilverStripe\Assets\Image; use SilverStripe\Assets\Image;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FileHandleField; use SilverStripe\Forms\FileHandleField;
use SilverStripe\Forms\NumericField; use SilverStripe\Forms\SearchableDropdownField;
use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
@ -23,16 +22,14 @@ use SilverStripe\ORM\DataObject;
*/ */
class DBForeignKey extends DBInt class DBForeignKey extends DBInt
{ {
/** /**
* @var DataObject * @var DataObject
*/ */
protected $object; protected $object;
/** /**
* This represents the number of related objects to show in a dropdown before it reverts * Number of related objects to show in a dropdown before it switches to using lazyloading
* to a NumericField. If you are tweaking this value, you should also consider constructing * This will also be used as the lazy load limit
* your form field manually rather than allowing it to be scaffolded
* *
* @config * @config
* @var int * @var int
@ -47,6 +44,7 @@ class DBForeignKey extends DBInt
* Cache for multiple subsequent calls to scaffold form fields with the same foreign key object * Cache for multiple subsequent calls to scaffold form fields with the same foreign key object
* *
* @var array * @var array
* @deprecated 5.2.0 Will be removed without equivalent functionality to replace it
*/ */
protected static $foreignListCache = []; protected static $foreignListCache = [];
@ -77,52 +75,13 @@ class DBForeignKey extends DBInt
} }
return $field; return $field;
} }
$labelField = $hasOneSingleton->hasField('Title') ? 'Title' : 'Name';
// Build selector / numeric field
$titleField = $hasOneSingleton->hasField('Title') ? 'Title' : 'Name';
$list = DataList::create($hasOneClass); $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
$threshold = self::config()->get('dropdown_field_threshold'); $threshold = self::config()->get('dropdown_field_threshold');
$overThreshold = $list->count() > $threshold;
// Add the count of the list to a cache for subsequent calls $field = SearchableDropdownField::create($this->name, $title, $list, $labelField)
if (!isset(static::$foreignListCache[$hasOneClass])) { ->setIsLazyLoaded($overThreshold)
// Let the DB do the threshold check as it will be faster - depending on the SQL engine it might only have ->setLazyLoadLimit($threshold);
// to count indexes
$dataQuery = $list->dataQuery()->getFinalisedQuery();
// Clear order-by as it's not relevant for counts
$dataQuery->setOrderBy(false);
// Remove distinct. Applying distinct shouldn't be required provided relations are not applied.
$dataQuery->setDistinct(false);
$dataQuery->setSelect(['over_threshold' => '(CASE WHEN count(*) > ' . (int)$threshold . ' THEN 1 ELSE 0 END)']);
$result = $dataQuery->execute()->column('over_threshold');
$overThreshold = !empty($result) && ((int) $result[0] === 1);
static::$foreignListCache[$hasOneClass] = [
'overThreshold' => $overThreshold,
];
}
$overThreshold = static::$foreignListCache[$hasOneClass]['overThreshold'];
if (!$overThreshold) {
// Add the mapped list for the cache
if (!isset(static::$foreignListCache[$hasOneClass]['map'])) {
static::$foreignListCache[$hasOneClass]['map'] = $list->map('ID', $titleField);
}
$field = new DropdownField($this->name, $title, static::$foreignListCache[$hasOneClass]['map']);
$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; return $field;
} }