【问题标题】:Update the SiteTree_Localised table from a script从脚本更新 SiteTree_Localised 表
【发布时间】:2019-02-26 12:22:31
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: php silverstripe silverstripe-4


    【解决方案1】:

    SiteTree_Localised 数据库表不能通过 ORM 直接访问,而是通过 Fluent 的 ORM 操作封装在钩子下。您可以通过更改“FluentState”然后像通常在 SilverStripe 中那样写入记录来访问和修改此表中的记录。

    这是您在 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 不会直接帮助您,但可能会提供一些见解。

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 1970-01-01
      • 2020-03-07
      • 2010-10-17
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多