Hello,
I have a page type "CentersPage" that shows centers in an accordion style. This page type has a "has_many" relationship with two data objects "Center" and "Location". Each accordion option has title (location) that when clicked on shows a table of centers that belong to that location.
<?php
class CentersPage extends Page {
static $has_many = array(
'Locations' => 'Location',
'Centers' => 'Center'
);
}
class CentersPage_Controller extends Page_Controller {
function centerLocations () {
$Locations = DataList::create('Location');
if ($Locations) {
foreach ($Locations as $Location) {
$dosSubSet = DataList::create('Center')->filter('LocationID', $Location->ID);
$Location->Centers = $dosSubSet;
}
}
return $Locations;
}
}
<?php
class Center extends DataObject {
static $db = array(
'Title' => 'Varchar',
'Description' => 'Text'
)
static $has_one = array(
'Page' => 'CentersPage',
'Location' => 'Location'
);
}
<?php
class Location extends DataObject {
public static $db = array(
'Title' => 'Varchar'
);
}
and in the template CentersPage.ss I have the following code:
<div id="accordion">
<% if centerLocations %>
<% loop centerLocations %>
<h2>$Title</h2>
<% loop Centers %>
<h3>$Title</h3>
<p>$Description</p>
<% end_loop %>
<% end_loop %>
<% end_if %>
</div>
I'm using the above code to loop through the Location dataobject and inside of it I'm looping through Center dataobject. The problem I'm having is I can't get the centers to show inside their location.. any help?
thanks