Domanda

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?

È stato utile?

Soluzione

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();

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top