Jump to:

22977 Posts in 11806 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Adding objects (not pages) as a child of a (Parent) page

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: 488 Views
  • wilsonStaff
    Avatar
    Community Member
    142 Posts

    Adding objects (not pages) as a child of a (Parent) page Link to this post

    Hi to all, first i know about DataObjectsAsPages, but was wondering if there is something simplier that i can use, i.e. custom php fonction.

    Usually, if i need children of a parent page, i add a page in the site tree that are related to the parent page:
    MY SONGS
    - Help me
    - Shout
    - Building this town

    where each children page have customs textfields to track .mp3's, titles etc.

    But this quickly loads the site tree.

    - - -

    What i need is a trick that will allow me to add (via a tab in the CMS) a child to any parent page (or to a precise parent only).

    These children:
    - wouldnt be visible in the sitetree
    - could be controlled via <% control .... %> in the templates
    - could bear any parameters in need: textfield to track .mp3, .mov, .webm, etc.

    Is this exactly what DataObjectsAsPages do?

    Thanks!

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Adding objects (not pages) as a child of a (Parent) page Link to this post

    What you're after is a DataObject class. The page itself would be the parent extending Page and the children extend DataObject. You link them by defining the has_many static on the Page and the has_one static on the child DataObject class.

    For example, it might look like this:

    <?php
    class SongPage extends Page {

       public static $has_many = array(
          'Songs' => 'Song'
       );

       public function getCMSFields() {
          $fields = parent::getCMSFields();

          // insert a table allow adding/editing/deleting children Song DataObjects

          return $fields;
       }

    }
    class Song extends DataObject {

       public static $db = array(
          'Title' => 'Varchar(100)',
          'Format' => 'Enum("wav,mp3,ogg")'
       );

       public static $has_one = array(
          'SongPage' => 'SongPage'
       );

    }

    Of course I've missed off the bit where you enter the table in the CMS to add/edit/delete the related Song DataObjects.

    Are you using SilverStripe 3.0? If so, you'd add a GridField in there which allows you to do that.
    On 2.4 you have to use a ComplexTableField.

    Sean

  • wilsonStaff
    Avatar
    Community Member
    142 Posts

    Re: Adding objects (not pages) as a child of a (Parent) page Link to this post

    Hi Sean, i am using either SS3.0.x or 2.4.x depending upon if its a new site or an update.

    Ive builded a couple of sites with SS3, and so far its gooing well - minus the non-working Get-Image-From-The-FIle-Store.

    - - -

    Back to question, can you point mw toward some tutorials about how to add GridField (SS3) or ComplexTextField(SS2.4.x). What i need is a that like the Content / Parameters / etc tabs.

    Thanks!

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: Adding objects (not pages) as a child of a (Parent) page Link to this post

    Check out the docs for a rundown on GridField http://doc.silverstripe.org/framework/en/reference/grid-field.

    For a quick how to... GridFields are amazingly easy to setup. Using Sean's code:

    <?php
    class SongPage extends Page {

    public static $has_many = array(
    'Songs' => 'Song'
    );

    public function getCMSFields() {
    $fields = parent::getCMSFields();

    $fields->addFieldToTab('Root.SongManager', GridField::create('Songs', 'Song', $this->Songs(), GridFieldConfig_RecordEditor());

    return $fields;
    }

    }

    488 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.