Silverstripe Version: 5.3
Question:
I’ve been following the polymorphic $has_one example from documentation
With regards to the following:
An additional column is created called <relationship-name>Class
, which along with the <relationship-name>ID
column identifies the object.
When does the <relationship-name>Class
field actually get set or do I need to add some code to do this? The <relationship-name>ID
field gets set when I select an item from a dropdown (with field name <relationship-name>ID
), but how do I also set the <relationship-name>Class
field from this? It certainly isn’t being set automatically and just defaults to the first ClassName available.
Any tips would be much appreciated.
It depends on how the relation is being set.
- If using
DataObject::setComponent()
it’s done in that method.
- If adding directly to a
PolymorhpicHasManyList
it’s done in the add()
method.
- If using one of the
SearchableDropdown*Field
classes it’s done in the saveInto()
method.
- For uploading files it’s done in the
FileUploadReciever::saveInto()
method.
- If creating a new record in a GridField it may be done via
GridFieldDetailForm_ItemRequest::ItemEditForm()
Regular dropdowns may not be an appropriate way of adding polymorphic relations as they’re usually only aware of a single field (e.g. MyRelationID
) not the actual relation itself.
1 Like
Thanks - that all makes sense. SearchableDropdownField sounds like what I’m after.
Do you have a working example of it in use with a polymorphic $has_one? I’m struggling to find anything in the documentation on it.
I don’t, but it should be pretty straight forward to work out by looking at the API surface. If you come across any specific problems setting it up I’m sure posting here will get you some help.
1 Like