Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Dynamically extending $db of DataObject to get multilingual support

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 731 Views
  • jpeuker
    Avatar
    Community Member
    1 Post

    Dynamically extending $db of DataObject to get multilingual support Link to this post

    Hello everyone!

    I am quite new to SilverStripe and these forums so I hope this is the correct board to ask this question.

    I recently started working with the Translatable extension and ran into the common known problems with the DataObjects. However I had an idea on how it would be possible to deal with this problem.

    The basic idea is that a DataObject should be the same in every language except for the TextFields (which contain different content in different languages).
    I created a new class "TranslatableDataObject" which directly extends "DataObject". This class contains a function which checks the $db array for "Text" objects and duplicates them for each available locale (fetched from Tanslatable::get_allowed_locales();)

    The target is to be able to create a TranslatableDataObject and fill $db with sth. like "Name" and "Photo" leading SilverStripe to create a Database table with one column for the "Photo" and one column per available locale for "Name".

    Therefore the class I wrote calls the previously described function just before the parent constructor is called.

    Here is what I did: /E: Edited the alterdb function, it should now work as expected... however as it is not working properly at the moment there seem to be some problems with the context...

    <?php

    class TranslatableDataObject extends DataObject {

    //This function alters the $db array according to the needs of multilingual handling

    function alterdb($db, $allowed_locales) {

    if ($db) {

    $dbnew = array();

    $keys = array_keys($db);

    $counter = 0;

    foreach ($db as $current) {

    if ($current == 'Text') {

    //Found TextField, create new textfields for each allowed locale, schema: TextFieldName_Locale

    foreach ($allowed_locales as $currentlocale) {

    $indexname = $currentlocale."_"."$keys[$counter]";

    $dbnew[$indexname] = $current;

    }

    }

    else {

    $dbnew[$keys[$counter]] = $current;

    }

    $counter = $counter + 1;

    }

    return $dbnew;

    }

    }

    function __construct() {

    $db = self::$db;

    $allowed_locales = Translatable::get_allowed_locales();

    self::$db = $this->alterdb($db,$allowed_locales);

    parent::__construct();

    }

    }

    ?>

    However I am just receiving errors using classes extending this class. (Although a DB rebuild is working fine) I am still working on this but I am a little stuck at the moment.

    As soon as this works I will start to work on some further code to hide all information from getCMSFields_forPopup which are not related to $current_lcoale.

    Any comments appreciated

    731 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.