2014-05-05 74 views
0

Silverstripe中有兩個名爲TeamPage和TeamReport的自定義頁面類型。在Silverstripe頁面類型上自動創建子頁面

當創建一個新的TeamPage時,我希望TeamReport作爲單個子頁面添加 - 以'/ report /'作爲URL slug。

我應該爲TeamPage添加什麼代碼才能發生這種情況?

回答

5

爲此,我會在onBeforeWrite中檢查創建之後,在TeamPageonAfterWrite方法中創建TeamReport

public function onBeforeWrite() { 
    parent::onBeforeWrite(); 
    $this->IsCreating = !$this->ID; 
} 

public function onAfterWrite() { 
    parent::onAfterWrite(); 
    if ($this->IsCreating) { 
     $child = new TeamReport(); 
     $child->ParentID = $this->ID; 
     $child->URLSegment = 'report'; 
     $child->Title = 'Report'; 
     $child->write(); 
    } 
}