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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

Listing titles from a many-many relationship

Go to End

4 Posts   2623 Views


Community Member, 21 Posts

21 February 2009 at 6:40am

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,


Community Member, 21 Posts

21 February 2009 at 6:50am

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


Community Member, 791 Posts

22 February 2009 at 2:27am


Looks like your broadcaster page class needs a

static $belongs_many_many to complete the relationship.

(check out : )

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

Good luck !


Community Member, 21 Posts

24 February 2009 at 1:15am

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,