I'm trying to create a series of regional pages that then list projects grouped by country in the particular region.
- I have a country table in the db with the following three fields: ID, Name, RegionPageID.
- I also have a projectpage table in the db with info about the projects.
- And finally I have a projectpage_country table that relates the two: ID, ProjectPageID, CountryID
I'm getting stuck on writing the foreach loop that gets the project info and combines it with the country info. None of the examples I'm finding show how to do this with two DataObjects.
function Countries() {
// get countries for the given region
$countryList = DataObject::get (
$callerClass = 'Country',
$filter = "RegionPageID = '{$this->ID}'",
$sort = 'name ASC'
);
$countryListIterator = $countryList->getIterator();
// loop through countries
foreach($countryListIterator as $country) {
// if the country has projects associated with it
// return the country name to the template along with the related projects (as a dataobject?)
}
// return something here
}
function Projects($countryID) {
$where = "projectpage_country.countryID = '$countryID'";
$join = "RIGHT JOIN projectpage_country ON ProjectPage_Live.ID = projectpage_country.projectPageID";
return DataObject::get('ProjectPage', $where, 'endDate DESC, title ASC', $join);
}
Then in the template...
<% control Countries %>
<h3>$CountryName</h3>
<% control Projects %>
<% if first %>
<ul>
<% end if %>
<li><a href="$Link">$Title</a> ($startDate.Year–$endDate.Year)</li>
<% if last %>
</ul>
<% end if %>
<% end_control %>
Any help would be most appreciated. Thanks.
Cheers,
Laurie