Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Dropdown in ModelAdmin changes to input


Go to End


2 Posts   904 Views

Avatar
StuM

Community Member, 59 Posts

22 February 2017 at 3:53pm

Hi All,

I have a DataObject with a has one association that used to always show a dropdown in the CMS, but today changes to a text input just showing it's ID. All that happened that I noticed were some more associated DataObjects added. There are now 302, so I'm wondering if there's a limit, and if there is can I increase that somehow?

I also double checked with my dev version which only has 50 associated records running the same code and it still shows the dropdown.

Avatar
Devlin

Community Member, 344 Posts

25 February 2017 at 7:47am

Edited: 25/02/2017 7:49am

The ModelAdmin will produce a NumericField instead of a DropdownField for has_one relations if the their amount is larger 100. Because it "might exceed the available PHP memory in creating too many DataObject instances." Unfortunately this is hard coded. (@see ForeignKey->scaffoldFormField())

To workaround that you'll need to specify the relation field in getCMSFields() yourself. e.g.:

class MyRecord extends DataObject {
	private static $has_one = [
		'Category' => 'MyCategory',
	];
	public function getCMSFields() {
		$fields = new FieldList(new TabSet('Root'));

		$cat = DropdownField::create('CategoryID', 'Category')
				->setSource(MyCategory::get()->map('ID', 'Title'))
				->setHasEmptyDefault(true);
		$fields->addFieldToTab('Root.Main', $cat);

		return $fields;
	}
}