Skip to first unread message

Nicolaas Thiemen Francken

Mar 1, 2017, 5:18:35 PM3/1/17
to silverstripe-dev

Does anyone have an example of PolymorphicForeignKey usage?  

We tend to use two DB fields (MyRelationClassName + MyRelationID) for relationships to DataObjects of various types (for example, right now, I am adding a Note DataObject which has_one parent, where parent can be one of several DataObject types). 

Obviously saving an arbitrary DataObject as one field without extra code would be great, especially if you could specify the types of DataObjects allowed.

Any ideas appreciated.


Sam Minnée

Mar 1, 2017, 5:31:10 PM3/1/17
to silverstripe-dev
2 fields is the best way to store this, as the class can be an enum and the ID can be an int. this allows for better indexing. A single, eg, string field would lead to slower index look-ups

PolymorphicForeignKey should generate a pair of database fields in this way, and treat it as one property on your dataobject.

Does that make sense?
You received this message because you are subscribed to the Google Groups "SilverStripe Core Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to silverstripe-d...@googlegroups.com.
To post to this group, send email to silverst...@googlegroups.com.
Visit this group at https://groups.google.com/group/silverstripe-dev.
For more options, visit https://groups.google.com/d/optout.

Sam Minnée


SilverStripe Limited


Phone: +64 4 978 7334

Twitter: @sminnee

Book a meeting with me

Reply all
Reply to author
0 new messages