Jump to:

23493 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Create children on Populate defaults

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

Page: 1
Go to End
Author Topic: 497 Views
  • pouic
    Avatar
    Community Member
    16 Posts

    Create children on Populate defaults Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Create children on Populate defaults Link to this post

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

  • pouic
    Avatar
    Community Member
    16 Posts

    Re: Create children on Populate defaults Link to this post

    i try but didn't work...

    When create the page in CMS no child is created

  • pouic
    Avatar
    Community Member
    16 Posts

    Re: Create children on Populate defaults Link to this post

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

    497 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.