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.

General Questions

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

Create children on Populate defaults


Reply

4 Posts   513 Views

Avatar
pouic

8 April 2012 at 9:40pm Community Member, 16 Posts

how to create pages and at the same time creating her children?

I try using populate defaults

public function populateDefaults(){
parent::populateDefaults();

$p = new Page();
$p->Title = 'TitlePage';
$p->MenuTitle = 'MenuTitle';
$p->URLSegment = $p->Title;
$p->Content = '';
$p->Status = 'Published';
$p->write();
$p->doRestoreToStage();
$p->publish('Stage', 'Live');
$p->flushCache();
}

Any idea to create this structure?

Avatar
Willr

8 April 2012 at 11:39pm Forum Moderator, 5511 Posts

A child is set by populating the ParentID with the ID of the parent

$parent = new Page();
$parent->Title = 'TitlePage';
$parent->MenuTitle = 'MenuTitle';
$parent->URLSegment = $parent->Title;
$parent->write();
$parent->publish('Stage', 'Live');

$child = new Page();
$child->Title = 'Child TitlePage';
$child->ParentID = $parent->ID;
$child->MenuTitle = 'Child MenuTitle';
$child->URLSegment = $child->Title;
$child->write();
$child->publish('Stage', 'Live');

Avatar
pouic

9 April 2012 at 4:45am Community Member, 16 Posts

i try but didn't work...

When create the page in CMS no child is created

Avatar
pouic

9 April 2012 at 5:04am Community Member, 16 Posts

using this code and work fine :)

public function populateDefaults(){
parent::populateDefaults();

$this->write();
$this->publish('Stage', 'Stage');

$child = new Page();
$child->Title = 'Child TitlePage';
$child->ParentID = $this->ID;
$child->MenuTitle = 'Child MenuTitle';
$child->URLSegment = $child->Title;
$child->write();
$child->publish('Stage', 'Stage');
}