$has_one not selecting saved record

Silverstripe Version: 4.1

I have this $has_one setup and I can see it’s saving in the tables okay and when I output in the controller the DropdownField is set correctly. However it doesn’t show the correct selection in the admin. Why?

In a Page class I have:

private static $has_one = [
	'ProductCategory' => ProductCategories::class
];

$fields->addFieldToTab('Root.Content', DropdownField::create('ProductCategory', 'Product category', ProductCategories::get()->map('ID', 'Title')));

That grabs categories and puts them into the DropdownField (chosen). I hit save/publish and look in ProductListPage, ProductListPage_Live and ProductListPage_Versions database tables and they all have ProductCategoryID set to the correct ID number of 2.

I hack an output in the Controller class that handles this page:

echo $this->ProductCategoryID;

That also shows “2”.

Go into the admin for this page though and the field is always set to have “1” selected.

replace ProductCategory with ProductCategoryID on dropdown field

private static $has_one = [
	'ProductCategory' => ProductCategories::class
];

$fields->addFieldToTab('Root.Content', DropdownField::create('ProductCategoryID', 'Product category', ProductCategories::get()->map('ID', 'Title')));
1 Like

Sonofa! :man_facepalming:

Thanks for catching that.

1 Like