Jump to:

5520 Posts in 1734 Topics by 1223 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Outputing data from a many to many relationship

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1616 Views
  • Andrew Houle
    Avatar
    Community Member
    132 Posts

    Outputing data from a many to many relationship Link to this post

    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 (ScheduleHolder.ss)

    <% control Children %>
          <h2>$Header</h2>
    <p>
       <% 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 %>
    </p>
    <% control Courses %>
       $CourseTitle
       <% control getFaculty %>
       $Name
    <% 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.

  • TT
    Avatar
    Community Member
    6 Posts

    Re: Outputing data from a many to many relationship Link to this post

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

    ...
    <% control Faculty %>
    $Name
    <% 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
    Avatar
    Community Member
    132 Posts

    Re: Outputing data from a many to many relationship Link to this post

    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

    1616 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.