Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Creating, publishing pages from code results in pages that cannot be edited or deleted?

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

Page: 1
Go to End
Author Topic: 1047 Views
  • johnmblack
    Avatar
    Community Member
    61 Posts

    Creating, publishing pages from code results in pages that cannot be edited or deleted? Link to this post

    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

  • johnmblack
    Avatar
    Community Member
    61 Posts

    Re: Creating, publishing pages from code results in pages that cannot be edited or deleted? Link to this post

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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Creating, publishing pages from code results in pages that cannot be edited or deleted? Link to this post

    try

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

    instead of

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

  • johnmblack
    Avatar
    Community Member
    61 Posts

    Re: Creating, publishing pages from code results in pages that cannot be edited or deleted? Link to this post

    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?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Creating, publishing pages from code results in pages that cannot be edited or deleted? Link to this post

    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

  • johnmblack
    Avatar
    Community Member
    61 Posts

    Re: Creating, publishing pages from code results in pages that cannot be edited or deleted? Link to this post

    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?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Creating, publishing pages from code results in pages that cannot be edited or deleted? Link to this post

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

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