从脚本更新 SiteTree_Localised table

Update the SiteTree_Localised table from a script

我已将 Silverstripe 网站从 3.x 升级到 4.x。

3.x 使用多语言进行翻译,现在 4.x 我已经用 Silverstripe Fluent 模块替换了多语言,但我想知道如何使用 Fluent 将数据(翻译字段)导入新系统到位。

更准确地说,我想以编程方式使用翻译后的数据更新 SiteTree_Localised table,例如

$record = SiteTree_Localised::get()->filter(array( 
      'RecordID' => 16, 
      'Locale' => 'de_DE' 
  ))->first(); 
$record->Title = 'Some title'; 
$record->write();

但这不起作用,因为 SiteTree_Localised 对象不存在?如何更新以上记录?

SiteTree_Localised 数据库table 无法通过 ORM 直接访问,但被 Fluent 的 ORM 操作包裹在钩子下。您可以通过更改 "FluentState" 然后像通常在 SilverStripe 中那样写入记录来访问和修改此 table 中的记录。

这就是您在 SilverStripe 4 中的特定 Fluent 上下文中编写对象的方式:

FluentState::singleton()->withState(function (FluentState $newState) {
    $newState->setLocale('de_DE');

    $record = SiteTree::get()->byID(16);
    $record->Title = 'Some title';
    $record->write();
});

您可以在一个循环中执行此操作(例如数据转储)并通过 use ($data, $locale) 将您需要的数据传递给 withState 回调。

还有一个 BuiltTask for migrating from translatable to fluent 不会直接帮助您,但可能会提供一些见解。