2

I'm trying to add pages to the Silverstripe Site tree using a background process that calls an external API.

I'm just trying to get the code to create a new Programme Page right. At the moment I have:

$mySiteTree = new SiteTree();
$mySiteTree->ClassName = "Programme";
$mySiteTree->URLSegment = $newurl;
$mySiteTree->URLSegment = 'testurl';
$mySiteTree->Title = 'testing title';
$mySiteTree->ShowInMenus = '1';
$mySiteTree->ParentID = '86';
$mySiteTree->write();

This doesn't seem to create any page. Any suggestions as to what I'm doing wrong or what I could try to do to debug this?

Martin T.
  • 445
  • 1
  • 7
  • 18
  • 1
    possible duplicate of [SilverStripe 3.1 - Page creating & publishing from the FrontEnd](http://stackoverflow.com/questions/21671943/silverstripe-3-1-page-creating-publishing-from-the-frontend) – Zauberfisch Feb 14 '14 at 13:51
  • A similar question has just been asked a couple of days ago. Please checkout http://stackoverflow.com/questions/21671943/silverstripe-3-1-page-creating-publishing-from-the-frontend - I suggest this to be closed as duplicate – Zauberfisch Feb 14 '14 at 13:52
  • SiteTree, should be page. A good reference is the built in default records.. https://github.com/silverstripe/silverstripe-cms/blob/3.1/code/model/SiteTree.php#L1419 – wilr Feb 15 '14 at 09:33

2 Answers2

7

There are a few things missing from your code and a few things that need to be fixed:

  • When creating the new page call new Programme() rather than new SiteTree()
  • You should not set the ClassName, the above change will take care of this
  • There is no need to set the URLSegment. Silverstripe will automatically do this based on the Title you set
  • Make sure a page with ID 86 exists
  • You need to call ->publish('Stage', 'Live') and ->flushCache()

The following code should work:

$mySiteTree = new Programme();
$mySiteTree->Title = 'testing title';
$mySiteTree->ShowInMenus = '1';
$mySiteTree->ParentID = '86';
$mySiteTree->write();
$mySiteTree->publish('Stage', 'Live');
$mySiteTree->flushCache();
3dgoo
  • 15,716
  • 6
  • 46
  • 58
  • Make sure you first change current Stage to "Stage". AFAIK the call to `$mySiteTree->write();` will otherwise write to Live as well. – bummzack Feb 28 '17 at 09:36
1

Have a look at the CMS Unit Tests. They're an excellent example of how to create pages and other records programmatically. This one in particular should get you started.

scrowler
  • 24,273
  • 9
  • 60
  • 92
cryptopay
  • 1,084
  • 6
  • 7