我用SilverStripe
3.1.13 (CMS和框架)构建了我的项目,我使用silverstripe-translatable
和dataobject-translatable
来组织我在网站上的翻译。
假设我有Post.php
(这是DataObject
在我的网站上与帖子一起工作),每个帖子都有自己的category
(many-many
关系在这里,但这并不重要))。
问题是:我在创建新的Post
数据对象时点击Save
按钮,我希望这些category
'ies将自动复制到另一个区域
我如何在我的应用程序中实现它?我想将这些值(它们是布尔值)保存到这些数据对象的另一个翻译中。
可能没有内置的功能,但是你可以在onAfterWrite()方法中写这样的东西:
public function onAfterWrite() {
$this->syncCategories();
}
public function syncCategories() {
//check if you're in main language, assuminig en_US here
if ($this->Locale !== 'en_US') return;
foreach ($this->getTranslations() as $translatedPage) {
//sync relations here
$translatedPage->RelationName = $this->RelationName;
//maybe more fine grained locic for only publishing when translated page is alredy published
$translatedPage->doPublish(); //writes and publishes in one
}
}