Jump to:

23552 Posts in 19349 Topics by 2891 members

General Questions

SilverStripe Forums » General Questions » A DataObject that has many of itself (related pages)

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

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

Page: 1
Go to End
Author Topic: 236 Views
  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    A DataObject that has many of itself (related pages) Link to this post

    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.

  • stallain
    Avatar
    Community Member
    61 Posts

    Re: A DataObject that has many of itself (related pages) Link to this post

    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

    236 Views
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.