Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Create Child SiteTree under Parent via PHP

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1407 Views
  • wee-man
    Avatar
    Community Member
    21 Posts

    Create Child SiteTree under Parent via PHP Link to this post

    Hello,

    in the Silverstripe CMS Backend, you are able to create new pages using the tree view on the left side.

    Now my intention is to create new pages with the use of formular data. Users should be able to create a new page without using the backend.

    My first try is:

    function doSubmit() {
    $subpage = new Page();
    $subpage->setParent($this); //also tried $subpage->parentID = $this->ID;
    $subpage->Title = ".."; //set attributes
    ...
    $subpage->save();
    }

    The function is in the Holder of Page and gets called by the form. Page and PageHolder are extended from SiteTree.

    My problem is, that after the function was called, there is no new subpage. In the database is only a new entry in "SiteTree", but without any fields filled (all NULL).

    Would be nice if someone has a clue what i'm doing wrong or gives a description to create subpages.

    Greetings

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Create Child SiteTree under Parent via PHP Link to this post

    You were on the right track but save() is incorrect. How I would do that is

    function doSubmit() {
    $subpage = new Page();
    $subpage->ParentID = $this->ID;
    $subpage->Title = "Page";
    $subpage->write();

    // if you want to publish the page to the site then you will need
    $subpage->publish('Stage', 'Live');
    }

  • wee-man
    Avatar
    Community Member
    21 Posts

    Re: Create Child SiteTree under Parent via PHP Link to this post

    Thanks, after a couple hours of sleep it's now working.
    The point was the publishing function.

    I also found something about creating pages in the recipes: http://doc.silverstripe.org/doku.php?id=recipes:examples_of_creating_and_saving_pages.

    PS: I used write() - save() was a typing error, sorry ;)

    1407 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.