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.

Data Model Questions /

Create Child SiteTree under Parent via PHP


Reply


3 Posts   1444 Views

Avatar
wee-man

Community Member, 21 Posts

25 November 2009 at 8:53am

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

Avatar
Willr

Forum Moderator, 5513 Posts

25 November 2009 at 11:58am

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

Avatar
wee-man

Community Member, 21 Posts

25 November 2009 at 9:59pm

Edited: 25/11/2009 9:59pm

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: [url]http://doc.silverstripe.org/doku.php?id=recipes:examples_of_creating_and_saving_pages[/url].

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