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.

Data Model Questions /

SS3 many-to-many relations


Reply


2 Posts   1752 Views

Avatar
spierala

Community Member, 80 Posts

21 July 2012 at 4:22am

Edited: 31/07/2012 9:02am

Hello all,
I just try to set up a many-to-many relation between two Dataobjects. Let´s say Events and Countries.
An event can be assigned to many countries and one country could be assigned to many events.

The relation setup seems to work basically. I have a Gridfield in the EventsPage showing all Events:

//EventItem.php
static $many_many = array(
'CountryItems' => 'CountryItem'
);

//CountryItem.php
static $belongs_many_many = array(
'EventItems' => 'EventItem'
);

//EventPage.php
function getCMSFields() {
$fields = parent::getCMSFields();
$gridfield = new GridField("Events", "My Events", $this->Events(), new GridFieldConfig_RelationEditor());
$fields->addFieldToTab('Root.Events', $gridfield);
return $fields;
}

If I click an Event there appears a Tab in the right top corner showing the countries. I can add and delete countries there. But they are only assigned to a single event (the one that was previously selected). I need a country list showing all countries that were ever created and with a checkbox for each country. so I can add and remove countries with just setting or deleting the relation. I think in former days it was called ManyManyComplexTableField...

any help is highly appreciated.

many thanx,
florian

Avatar
Futureweb

Community Member, 233 Posts

14 October 2012 at 1:35pm

on the event page you're setting up the grid field, but getting events with $this->Events()
however, there is nothing to connect to the event to the event page?

I guess 2 options - link to the 'event' to the 'event page' with many_many or has_many relation, or make 'event' into 'eventpage' so each event is it's own page

I've written up some notes on this here [url]http://www.silverstriperesources.com/articles/silverstripe-3-many-many-a-comprehensive-example/[/url]

I've actually put a "getCMSFields() { }" function on both sides to control it a bit better