4.1
Do polymorphic relationships work in Silverstripe V4.1?
I have two classes (Client & Campaign) that both have many of a third class (Value). My thought was that I could set up a polymorphic relationship so that class Value has a has_one relationship to a data object and class Client and class Campaign has a has_many relationship to class Value.
The relevant code snippet is below.
Everything seems to work, model admin allows me to add value items to both the other classes, however, on querying them ($this->Values()->Count()), they result in zero items found.
Looking at the Value database record in PHPMyAdmin, it would appear that the fields for ValueOfClass and ValueOfID are created to support the relationship but they are not set.
The code is created based on the documentation at Relations between Records – SilverStripe Documentation
Any help would be appreciated as I have been struggling with this all day!
class Client extends DataObject {
private static $table_name = 'Client';
private static $has_many = [
"Values" => "Value.ValueOf"
];
}
class Campaign extends DataObject {
private static $table_name = 'Campaign';
private static $has_many = [
"Values" => "Value.ValueOf"
];
}
class Value extends DataObject {
private static $table_name = 'Value';
private static $has_one = [
"ValueOf" => DataObject::class
];
}