문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top