简体   繁体   中英

Update the SiteTree_Localised table from a script

I have upgraded a Silverstripe site from 3.x to 4.x.

3.x was using Multilingual for translation, now in 4.x I have replaced Multilingual with Silverstripe Fluent module but I am wondering how I can import data ( translated fields ) into the new system with Fluent in place.

To be more precise I want to update the SiteTree_Localised table with the translated data programmatically something like

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

But this does not work as SiteTree_Localised object does not exist ? How do I update the above record ?

The SiteTree_Localised database table is not directly accessible via the ORM, but is wrapped up under the hook with Fluent's ORM manipulation. You can access and modify the records in this table by altering the "FluentState" then writing the record as you usually would in SilverStripe.

This is how you'd write objects in a specific Fluent context in SilverStripe 4:

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

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

You can do this in a loop (of a data dump for example) and pass the data you need in via use ($data, $locale) to the withState callback.

There's also a BuiltTask for migrating from translatable to fluent which won't help you directly but might provide some insight.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM