3093 Posts in 875 Topics by 654 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1562 Views |
-
Listing titles from a many-many relationship

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,
chris -
Re: Listing titles from a many-many relationship

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 31so 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....
-
Re: Listing titles from a many-many relationship

22 February 2009 at 2:27am
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 !
-
Re: Listing titles from a many-many relationship

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,
chris
| 1562 Views | ||
|
Page:
1
|
Go to Top |


