21310 Posts in 5739 Topics by 2604 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, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 268 Views |
-
Adding objects (not pages) as a child of a (Parent) page

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 townwhere 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!
-
Re: Adding objects (not pages) as a child of a (Parent) page

20 December 2012 at 9:09pm Last edited: 20 December 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
-
Re: Adding objects (not pages) as a child of a (Parent) page

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!
-
Re: Adding objects (not pages) as a child of a (Parent) page

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;
}}
| 268 Views | ||
|
Page:
1
|
Go to Top |



