gpt4 book ai didi

silverstripe - 如何在 SilverStripe 中的版本化数据对象上使用类似 onAfterPublish() Hook 的功能

转载 作者:行者123 更新时间:2023-12-02 07:47:00 24 4
gpt4 key购买 nike

我在 SilverStripe 中有一个简单的版本数据对象。我试图 Hook 发布操作并在发布数据对象时发送一封电子邮件。

我认为 onAfterPublish() 方法在数据对象(仅限页面)上不可用,因此我希望模仿该方法,或者在 onAfterWrite( ) 函数。

这是我目前的代码:

static $has_written = false; // Hack so it only fires once on write()
public function onAfterWrite()
{
parent::onAfterWrite();
if (!self::$has_written) {
$stage = $this->getSourceQueryParam("Versioned.stage");
if ($stage === 'Live') {
$email = new Email();
...
$email->send();
}
}
self::$has_written = true;
}

最佳答案

用于版本控制 DataObjectsVersioned 类没有 onAfterPublish Hook ,但它有一个 onBeforeVersionedPublish hook可用于发送电子邮件:

public function onBeforeVersionedPublish($fromStage, $toStage, $createNewVersion = false) {
$email = Email::create();
// ...
$email->send();
}

关于silverstripe - 如何在 SilverStripe 中的版本化数据对象上使用类似 onAfterPublish() Hook 的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35996117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com