Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Listing titles from a many-many relationship

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

Page: 1
Go to End
Author Topic: 1815 Views
  • chris_d
    Avatar
    Community Member
    21 Posts

    Listing titles from a many-many relationship Link to this post

    First of all, thanks for answering my last post.

    I'm building a site for a film distributor, and I'm trying to make a page for their programmes that allows them to associate each program with a number of broadcasters.

    I've got a many-many relation between a ProgrammePage (contains content,photos and a list of broadcasters) and Broadcasters page types (just contains a title), and a checkbox field of all the Broadcasters that can be associated with it.
    So this all stores fine, but I'm trying to make the layout page for the ProgrammePage display the title of all the broadcasters.

    When I had just a 1-1 relationship I could just put the following function my ProgrammePage.php and call it with <% control ReturnBroadcaster %>

    function ReturnBroadcaster() {
    return DataObject::get_by_id("Broadcaster",$this->BroadcasterID);
    }

    But now I've got a many-many I believe the relationships are no longer stored in the programmepage table (in programmepage_broadcaster instead I think) and Im a bit stuck as to where I should be pulling out the data from.

    So basically my question is how do I list the titles of all the associated objects from a many-many relationship?

    My ProgrammePage.php:

    static $many_many = array(
       'Broadcasters' =>    'Broadcaster'   
    );

    ...

    function getCMSFields() {
    ...
    $categoryList = DataObject::get('Broadcaster');
    $fields->addFieldToTab('Root.Content.Broadcasters', new CheckboxSetField('Broadcasters', '', $categoryList));
    ...
    }

    My Broadcaster.php:

    ...

    static $many_many = array(
       'ProgrammePages' =>    'ProgrammePage'   
    );
    ..

    Thanks for any help you can provide,
    chris

  • chris_d
    Avatar
    Community Member
    21 Posts

    Re: Listing titles from a many-many relationship Link to this post

    hm.. looks like all the info is stored in programmepage_broadcasters eg;

    ID   ProgrammePageID   BroadcasterID
             1   19   27
             2   19   14
             3   19   20
             4   19   31

    so I guess I'll need to write an SQL query to run through returning the title of any broadcasters that are associated with the programmeid....

  • Fuzz10
    Avatar
    Community Member
    786 Posts

    Re: Listing titles from a many-many relationship Link to this post

    Chris..

    Looks like your broadcaster page class needs a

    static $belongs_many_many to complete the relationship.

    (check out : http://doc.silverstripe.org/doku.php?id=recipes:many_many-example )

    If you have done this, you can get a list of all your Broadcasterpages by using getManyManyComponents('xxxx'); from within your ProgrammePage ..

    Good luck !
       

  • chris_d
    Avatar
    Community Member
    21 Posts

    Re: Listing titles from a many-many relationship Link to this post

    thanks fuzz10!

    i put this in my ProgrammePage.php:

    function ReturnBroadcasters() {
    return $this->getManyManyComponents('Broadcasters');
    }

    and in my ss

    <% control ReturnBroadcasters %>
    <img src="$Photo.URL" alt="$Title"><br>
    <% end_control %>

    thanks again,
    chris

    1815 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.