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

Adding objects (not pages) as a child of a (Parent) page


Go to End


4 Posts   987 Views

Avatar
wilsonStaff

Community Member, 143 Posts

20 December 2012 at 8:45am

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!

Avatar
Sean

Forum Moderator, 922 Posts

20 December 2012 at 9:09pm

Edited: 20/12/2012 9:13pm

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

Avatar
wilsonStaff

Community Member, 143 Posts

21 December 2012 at 1:44am

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!

Avatar
copernican

Community Member, 189 Posts

21 December 2012 at 5:53am

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; 
   }

}