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

Create children on Populate defaults


Go to End


4 Posts   1480 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, 5523 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');
    }