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   553 Views

Avatar
pouic

Community Member, 19 Posts

8 April 2012 at 9:40pm

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

Forum Moderator, 5513 Posts

8 April 2012 at 11:39pm

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

Community Member, 19 Posts

9 April 2012 at 4:45am

i try but didn't work...

When create the page in CMS no child is created

Avatar
pouic

Community Member, 19 Posts

9 April 2012 at 5:04am

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