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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

A DataObject that has many of itself (related pages)

Go to End

2 Posts   1025 Views


Community Member, 607 Posts

20 February 2014 at 10:19pm

Edited: 20/02/2014 10:43pm

SS 2.4.x

Weird title. Here's the easiest way I can explain what I need:

I have a page type called Tour. Extends BaseTour.
A Tour can have many AddOns.
An AddOn is a Tour

Really, think about it like a related page system. But searches for that didn't bring up much.

What we want is when a Tour is open in the CMS, we have a tab for "Tour Addons". This will list all Tours already in the system with checkboxes to select as many as needed. (ManyManyComplexTableField). These Tours should NOT be added via this tab. If a new Tour is needed, it is added as a page in the SiteTree.

The display side of things in the CMS isn't the problem. What I am trying to work out is how to reference the TourAddOn object to Tour and vice versa.

Current code snippets:


// Many to many object relationships
	static $many_many = array(
		'TourOptions' => 'TourOption',
		'TourStyles' => 'TourStyle',
                'TourAddOns' => 'TourAddOn'
function getCMSFields() {
		$fields = parent::getCMSFields();

        // CMS admin popop for options (option name and one field per currency price)
		$tourAddOnsTablefield = new ManyManyComplexTableField(
                             // Properties from the Tour object?
			"" //sort
		$tourAddOnsTablefield->setAddTitle( 'A Tour Add On' );

TourAddOn (whole)


 * Tour Add Ons take existing tours in the system and offer them on other tours.
 * So a Tour can have many TourAddOns
 * And a TourAddOn can have many Tours
class TourAddOn extends DataObject {
   static $has_many = array(
        'TourAddOns' => 'Tour'
    // Many to many object relationships
	static $belongs_many_many = array (
		'Tours' => 'Tour',

Any takers? If there's a related pages module or snippet floating around, I think that would be a perfect start. Would consider upgrading to 3.x if needed for this one.


Community Member, 68 Posts

21 February 2014 at 9:39pm

Edited: 21/02/2014 9:40pm


It's not a direct way to handle your problem, but I think I would consider creating another D.O. (let's call it "TourCategory").

-> A Tour can belong to many TourCategories (belongs_many_many relationship)
-> A TourCategory can have many Tours (many_many relationship)

When you create a new Tour page, you can attach it to one or more TourCategories ; and TourCategories could be managed through ModelAdmin, or a new tab on a parent page.

In your Tour page template, you can loop through the Tour's categories, then through each category's Tours, and that's it.