Silverstripe:默认值只设置一次


Silverstripe: Set default values only once

嗨,我正试图设置一个文章页面的默认值(一次)。因为它是一种博客系统,我使用日期和时间。日期和时间是集成的,但每次我更改和更新页面,日期和时间也改变了。也就是说,url变了。我如何修改populateDefaults只更新一次?onBeforeWrite有机会吗?因为一旦保存,页面应该不再是新的,但它一直在变化Mille grazie *

ArticlePage.php

<?php
class ArticlePage extends Page {
    private static $db = array(
        'Place' => 'Text',
        'Date' => 'Date',
    );

    public function populateDefaults(){
        parent::populateDefaults();
        // set title and urlsegment only once ?
        $this->Title = _t('Page.TITLE', "post-" . date("Ymd-His"));
        $this->URLSegment = _t('Page.URLSEGMENT', "post-" . date("Ymd-His"));
    }
    public function onBeforeWrite(){
        parent::onBeforeWrite();
        if ($this->Status == 'New page'){ // <-- should not be new after saving once
            $this->populateDefaults();
            }
        if ($this->Verison == 0){
            $this->populateDefaults();
            }       
    }
}

您只需要使用populateDefaults()函数。

Silverstripe会在创建页面时自动调用它…

private static $db = array(
    'Place' => 'Text',
    'Date' => 'Date',
);

public function populateDefaults(){
    parent::populateDefaults();
    // set title and urlsegment only once ?
    $this->Title = _t('Page.TITLE', "post-" . date("Ymd-His"));
    $this->URLSegment = _t('Page.URLSEGMENT', "post-" . date("Ymd-His"));
}

}