Silverstripe Version: 5.1
If I have the following DataObjects and relations:
class ProjectSector extends DataObject
{
private static $many_many = [
'Examples' => Example::class,
'OtherExamples' => OtherExample::class,
];
}
class Example extends DataObject
{
private static $has_one = [
'ProjectPost' => ProjectPost::class,
];
private static $belongs_many_many = [
'ProjectSectors' => ProjectSector::class,
];
}
class OtherExample extends DataObject
{
private static $has_one = [
'ProjectPost' => ProjectPost::class,
];
private static $belongs_many_many = [
'ProjectSectors' => ProjectSector::class,
];
}
class ProjectPost extends BlogPost
{
private static $has_many = [
'Examples' => Example::class,
'OtherExamples' => OtherExample::class,
];
//...
public function ProjectSectors()
{
$arrayList = ArrayList::create();
foreach (self::$has_many as $examplesRelationName) {
foreach ($this->{$examplesRelationName}() as $example) {
foreach ($example->ProjectSectors() as $dataObject) {
$arrayList->push($dataObject);
}
}
}
return $arrayList->removeDuplicates();
}
}
Question:
Is there an ORM method to list all ProjectSector records associated with a ProjectPost by proxy, i.e. through its Example & OtherExample relations?
I am currently using the ProjectSectors method on ProjectPost, but it ain’t pretty and I feel like I am missing something obvious here
Any pointers gratefully received!