Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

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

Dynamically extending $db of DataObject to get multilingual support

Go to End



Community Member, 1 Post

23 February 2011 at 2:16am

Edited: 24/02/2011 12:38am

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...


   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);





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 :)