Silverstripe Version: Silverstripe 4
Question:
I have two DataObjects, Location and Producer. I’ve established a many_many/belongs_many_many between them, like so:
Producer.php
private static $many_many = [
'Locations' => Location::class
];
Location.php
private static $belongs_many_many = [
'Producers' => Producer::class
];
In the Locations Page Controller, I am trying to join the data, so I can print it to the template.
LocationsPageController.php
$sqlQuery = new SQLSelect();
$sqlQuery->setFrom('Location');
$sqlQuery->addLeftJoin('Producer_Locations','`Location`.`ID` = `Producer_Locations`.`LocationID`');
$sqlQuery->addLeftJoin('Producer','`Producer`.`ID` = `Producer_Locations`.`ProducerID`');
$result = $sqlQuery->execute();
$LocationList = ArrayList::create();
foreach($result as $row) {
$LocationList->push(ArrayData::create($row));
}
I was hoping that the LocationList loop would contain a Producer Loop within each row, but all I am getting is Producer data.
Hopefully I’ve provided enough info. One thing that may be causing a problem is that each DataObject’s columns use the same column names, “Title”, “Description”, “Slug”, and “Sort”
I’ve read and reread as much documentation about many_many and belongs_many_many relationships, but I haven’t found enough info about joining and rendering the data to the template, to figure it out for myself.
Thanks for any help!
// Include any relevant code. If you have a lot of code, link to a gist instead.