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

Adding page from controller


Go to End


3 Posts   960 Views

Avatar
FullWebService

Community Member, 38 Posts

29 January 2010 at 5:46am

Hi,

I'm trying to add a page to the Sitetree from a controller.
I have ProductGroup which extends Page and I'm able to make ProductGroup pages in the CMS.

My code:

$pg = new ProductGroup;
$pg->Title = "Test";
$pg->ParentID = 2;
$pg->write();

This makes a valid entry in both Sitetree_Live and ProductGroup in the database and I can view that page in the website. It also makes an entry in Sitetree, but with the ClassName SiteTree, no Title and no ParentID (ParentID = 0). It kinda mucks things up in the CMS. The page is there, but in the root and not under node 2. It doesn't have an URLSegment so it's hard to click on and it's uneditable.

Is there any way I can have the Sitetree behave the same as Sitetree_Live?

TIA

Avatar
Willr

Forum Moderator, 5523 Posts

29 January 2010 at 12:17pm

With writing pages you need 2 lines. One to write, the other to sync the stage / live modes

$page->write();
$page->publish("Stage", "Live");

It doesn't have an URLSegment so it's hard to click on and it's uneditable.

So then you probably need to set its URLSegment when you create the object.

Avatar
FullWebService

Community Member, 38 Posts

29 January 2010 at 1:24pm

Edited: 29/01/2010 1:25pm

Thanks for the reply. The URLSegment is generated automatically so I don't have to set it. It just wasn't working in the Stage table.

I had to reverse Stage and Live in the publish function, as it was going well for the Live bit, but not the Stage bit.

$pg->publish("Live", "Stage");

made it work!

Thanks again.