Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

Outputing data from a many to many relationship


3 Posts   1642 Views

Andrew Houle

10 January 2009 at 7:00am Community Member, 132 Posts

I'm really frustrated with an issue I'm having; logically I think I have everything in order, so I'm puzzled with what I'm doing wrong, hopefully someone can help.

Here's the basic info. I have a page type called SaturdayPage, a page type called Faculty and a data object called course. The relationships are as follows:
A SaturdayPage has many Course and a Course belongs to many SaturdayPage
A Faculty has many Course and a Course has one Faculty

All the complex table fields work and the info seems to be stored properly in the DB. Where I run into trouble is when I try to use this simple function w/in Course.php:

function getFaculty() {
   $obj = 'Faculty';
      $sort = 'FirstName ASC';
      $filter = 'Faculty', "`ID` = '{$this->FacultyID}'";
      $join = '';
      $limit = '';
      $gf = DataObject::get($obj, $filter, $sort, $join, $limit);
      return $gf;   

In the database the FacultyID field is populated correctly, but this code renders nothing in the template. If I take out the $filter variable, I can populate all the Faculty objects. But even the simpliest of filters (ie "ID='1'";) breaks everything down, and I get an error.

Here is the code excerpt in the template (

<% control Children %>
   <% if StartDate %>$StartDate.Format(m)/$StartDate.Format(d)/$StartDate.Format(y)<% end_if %>
   <% if EndDate %> - $EndDate.Format(m)/$EndDate.Format(d)/$EndDate.Format(y)<% end_if %>
<% if SpecialComments %><br />$SpecialComments<% end_if %>
<% control Courses %>
   <% control getFaculty %>
<% end_control %>

<% end_control %>
   <% end_control %>

Please let me know any ideas you guys/gals may have. Also if it helps to show the pages and dataobject classes let me know.

Thanks in advance.


10 January 2009 at 10:03am (Last edited: 10 January 2009 10:04am), Community Member, 6 Posts

I think you do not have to use a special method giving you the relation. Following snipped should work:

<% control Faculty %>
<% end_control %>
The control gives you the related object (because in this direction it is similar to a 1-to-1 relation)

Otherwise following statement will give you the related Faculty object:
function getFaculty() {
return $this->getComponent('Faculty');

Your filter does not work because it is directly taken to the WHERE clause in the SQL statement. A simple
$filter = 'ID = ' . $this->FacultyID;
should also find the correct related object.

Andrew Houle

10 January 2009 at 10:32am Community Member, 132 Posts

You rock, thanks for the great advice! I'm glad I asked and then walked away for a bit. Sometimes the answer should be right in front of you, but you just can't see it. How I could not try that first simple control loop is beyond me, either way... the help is much appreciated :)