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 /

Creating, publishing pages from code results in pages that cannot be edited or deleted?


Reply


7 Posts   1257 Views

Avatar
johnmblack

Community Member, 61 Posts

28 July 2011 at 11:39am

Forgive me for such a potentially simple question; it seems like everyone's plugins "of course" know how to do this, but it is stumping me.

I have some code in a controller class that is simply trying to create SS pages and attach them to a parent in the tree. Based on what I find in the forums and API docs, I constructed the following bare-bones test case:

$parentPage = ContentController::Page('home'); // anything
$ssPage = new Page();
$ssPage->setParent($parentPage);
$ssPage->Title = 'My Test CodeCreated Page';
$ssPage->URLSegment = 'my-test-codecreated-page'; // $ssPage->generateURLSegment($ssPage->Title) works too
$ssPage->Content = 'some content';
$ssPage->write();
$ssPage->doPublish();
$ssPage->flushCache();

On the surface, it looks like it works. The page appears on the publicly-viewable site in the proper location, with the expected Title, Content, and url.

But in the CMS things start to go bad. Refreshing the CMS shows the page in what looks like a Deleted state (red, strikeout text). The only action button available is "Restore". And when I click it, I get "Server error". Even after all of that, the public page still views OK. But now I can't edit or delete it. (Well I could delete the appropriate records in the DB, but that's not the point.) :-)

Willr mentioned in one post that the code had to be in the data object. Does that mean the actual lines of code have to be inside the data object, or only that the above methods have to be called on a valid data object?

Thanks for any and all help.

-John

Avatar
johnmblack

Community Member, 61 Posts

28 July 2011 at 11:40am

BTW -- SS 2.4.5. And that line above about "generateURLSegment" is actually a wrapped comment from the previous line.

Avatar
swaiba

Forum Moderator, 1805 Posts

28 July 2011 at 6:16pm

try

$ssPage->writeToStage('Stage');
$ssPage->publish("Stage", "Live");

instead of

$ssPage->write();
$ssPage->doPublish();

Avatar
johnmblack

Community Member, 61 Posts

29 July 2011 at 2:21am

Hi swaiba, thanks for the response.

The method "writeToStage" is not found anywhere in the API documentation for any recent version on SiteTree, DataObject, ViewableData, and Object. What class is this a part of, and what version did it appear?

The docs are also not clear on the difference between "doPublish(void?)" and "publish(str, str...)". Is one called by the other?

Avatar
swaiba

Forum Moderator, 1805 Posts

29 July 2011 at 2:32am

Edited: 29/07/2011 2:32am

The method "writeToStage" is not found anywhere in the API documentation for any recent version on SiteTree, DataObject, ViewableData, and Object. What class is this a part of, and what version did it appear?

very close, but remember versioned is also applied to sitetree objects...
http://api.silverstripe.org/2.4/sapphire/model/Versioned.html#methodwriteToStage

The docs are also not clear on the difference between "doPublish(void?)" and "publish(str, str...)". Is one called by the other?

I've no idea, it's been a while since I wrote that piece of code, however you can load up your debugger and save a page and see what happens :)

Avatar
johnmblack

Community Member, 61 Posts

29 July 2011 at 3:53am

Thanks so much. To what extent is $newlyCreatedPage->flushCache(); necessary? I see it in some people's examples but not others. And is it done on the page that is created, or its parent?

Avatar
swaiba

Forum Moderator, 1805 Posts

29 July 2011 at 8:25pm

Since I never used it I'd say it wasn't required, but then I don't remember whether I needed to then flush the cache afterwards...