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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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   1568 Views

Avatar
Double-A-Ron

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:

Tour

...
// 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(
			$this,
			'TourAddOns',
			'TourAddOn',
			array(
                             // Properties from the Tour object?
			),
			'getCMSFields_forPopup',
			"",
			"" //sort
		);
		$tourAddOnsTablefield->setAddTitle( 'A Tour Add On' );

TourAddOn (whole)

<?php

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

Avatar
stallain

Community Member, 68 Posts

21 February 2014 at 9:39pm

Edited: 21/02/2014 9:40pm

Hi,

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.

Stan