Jump to:

3461 Posts in 1065 Topics by 740 members

Data Model Questions

SilverStripe Forums » Data Model Questions » SS3 many-to-many relations

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

Page: 1
Go to End
Author Topic: 1696 Views
  • spierala
    Community Member
    80 Posts

    SS3 many-to-many relations Link to this post

    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:

    static $many_many = array(
    'CountryItems' => 'CountryItem'

    static $belongs_many_many = array(
    'EventItems' => 'EventItem'

    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,

  • Futureweb
    Community Member
    233 Posts

    Re: SS3 many-to-many relations Link to this post

    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 http://www.silverstriperesources.com/articles/silverstripe-3-many-many-a-comprehensive-example/

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

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.