FuelPHP ORM数据库模式i18n,意见/建议 [英] FuelPHP ORM database schema for i18n, opinions/suggestions

查看:268
本文介绍了FuelPHP ORM数据库模式i18n,意见/建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然此问题可能是类似 < a href =http://stackoverflow.com/questions/316780/schema-for-a-multilanguage-database>到 很多 其他,我想询问关于FuelPHP上i18n规范的最佳方法的意见/建议。



因此,这里是我到目前为止:



资料库架构1:

 模型(id,name_pt,name_es,name_en,description_pt,description_es,description_en)



<范例资料#1:

 (1,'Modelo','Modelo','Model','Descrição 



优点:<模型描述> / strong>




  • 直接简单

  • 每个模型一个表格

  • 不需要使用JOIN

  • 使用魔法方法简化数据访问:



 公共函数& __get($ property)
{
if(array_key_exists($ property,array('name','description')))
{
$ property = $ property .'_ '.Session :: get('lang_code');
}

return parent :: __ get($ property);
}

这样,我可以调用:

  $ model-> name; 
$ model-> description;

而不是:

  $ model-> {'name _'。Session :: get('lang_code')}; 
$ model-> {'description _'。Session :: get('lang_code')};

缺点:




  • 有很多语言/翻译字段可能会混乱。

  • 添加新语言意味着向表中添加新字段

  • 魔法方法只有在我们已经有一个ORM实例/对象时才有效。通过查询生成器获取ORM实例,字段仍然需要如下代码:



 

  Model_Model :: query()
- > order_by('name _'。Session :: get('lang_code'))
- > get

资料库架构2:

 语言(id,code,name)
models(id)
i18n_models(id,model_id,language_id,name,description)

示例数据#2:

   -  languages 
(1,'pt','Português')
(2,'es','Español')
3,'en','English')

- models
(1)

- i18n_models
(1,1,1, 'modelo','Descriçãodo modelo')
(2,1,2,'Modelo','Descripcióndel modelo')
(3,1,3,'Model' )

优点:


$ b b

  • 更好的数据组织

  • 添加新语言非常简单

  • 也可以使用 set()方法进行直接数据访问,以填充$ _custom_data数组:



 

  $ i18n = Model_I18n_Model :: query()
- > where('model_id',$ model-> id)
- > where('language_id',Session :: get ('lang_code'))
- > get_one();

$ model-> set(array(
'name'=> $ i18n-> name,
'description'=> $ i18n-> description
));

缺点:




  • 复杂性增加

  • 必须使用JOIN或第二个查询

  • 必需



数据库模式3:



关于其他问题,我已经看到人们建议对所有翻译使用中央i18n表格,对每个翻译模型使用一行。



优点:




  • 模型之间共享i18n的单一表格

  • 添加新



缺点:




  • 在获取数据时,复杂性会增加,需要为每个模型的每个翻译文本加入一个JOIN

  • 我们可以尝试使用 EAV容器,尽管它使用键/值进行映射,但在这种情况下特别是我们还必须使用language_id来获取正确的翻译。



Personaly,我更喜欢第二种方法。你看到什么其他优点/缺点?有人在FuePHP上以不同的方式实现了i18n?分享您的想法:)

解决方案

我只需添加 lang 字段。



然后我对该字段进行过滤:

  SELECT * FROM articles WHERE lang ='en'

我甚至在CRUD中使用它用户可以切换语言的部分,并且它们查看该特定语言的所有条目。



编辑器将自动处理其所在语言的内容。

  INSERT INTO articles VALUES('My Title','My Article','en')

只需从用户当前本地获取'en'。 (我允许他们改变形式,虽然重写它)。


While this question might be similar to many others, I'd like to ask for opinions/suggestions on the best approach for i18n specificaly on FuelPHP.

So, here is what I've got so far:

Database schema #1:

models (id, name_pt, name_es, name_en, description_pt, description_es, description_en)

Sample data #1:

(1, 'Modelo', 'Modelo', 'Model', 'Descrição do modelo', 'Descripción del modelo', 'Model description')

Pros:

  • Straight and simple
  • One table per model
  • No need to use JOIN
  • Use of a magic method to simplify data access:

 

public function & __get($property)
{
    if (array_key_exists($property, array('name', 'description')))
    {
        $property = $property.'_'.Session::get('lang_code');
    }

    return parent::__get($property);
}

This way, I'm able to do call:

$model->name;
$model->description;

instead of:

$model->{'name_'.Session::get('lang_code')};
$model->{'description_'.Session::get('lang_code')};

Cons:

  • Having lots of languages/translated fields might get messy.
  • Adding a new language, implies adding new fields to the table
  • The magic method only works when we already have an ORM instance/object. To fetch an ORM instance through the query builder ordered by a translated field it's still required code like:

 

Model_Model::query()
    ->order_by('name_'.Session::get('lang_code'))
    ->get();

Database schema #2:

languages (id, code, name)
models (id)
i18n_models (id, model_id, language_id, name, description)

Sample data #2:

-- languages
(1, 'pt', 'Português')
(2, 'es', 'Español')
(3, 'en', 'English')

-- models
(1)

-- i18n_models
(1, 1, 1, 'Modelo', 'Descrição do modelo')
(2, 1, 2, 'Modelo', 'Descripción del modelo')
(3, 1, 3, 'Model', 'Model description')

Pros:

  • Better data organization
  • Adding a new language is a snap
  • Like in the first approach, we can also have a direct data access using the set() method to populate the $_custom_data array:

 

$i18n = Model_I18n_Model::query()
    ->where('model_id', $model->id)
    ->where('language_id', Session::get('lang_code'))
    ->get_one();

$model->set(array(
    'name' => $i18n->name,
    'description' => $i18n->description
));

Cons:

  • Complexity increases
  • A JOIN or a second query must be used
  • An extra table for each model is required

Database schema #3:

On other questions, I've seen people suggest the use of a central i18n table for all the translations, using a row for each translation a model has.

Pros:

  • Single table for i18n shared between models
  • Adding a new language should be easy as in the previous approach

Cons:

  • Complexity increases while fetching data, requiring a JOIN for every translated text a model has
  • We could try using EAV containers with this approach, although that uses a key/value for mapping, but in this case in particular we also have to use the language_id to fetch the proper translation.

Personaly, I prefer the second approach. What other advantages/disadvantages do you see? Has anyone implemented i18n in a different way on FuePHP? Share your ideas :)

解决方案

What I simply do is add a lang field in the the table.

Then I filter on that field:

SELECT * FROM articles WHERE lang = 'en'

I even use it in CRUD for admin sections where the user can switch languages, and they see all the entries for that specific language.

And an editor will be automatically working for content in the language he is in.

INSERT INTO articles VALUES('My Title', 'My Article', 'en')

And simply get 'en' from the users current local. (I do allow them to change in forms though to override it).

这篇关于FuelPHP ORM数据库模式i18n,意见/建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆