如何构建多语言使用的模型
我很好奇对于多语言环境来说,一个好的模型结构是什么。我将使用产品数据库作为示例....
products
----------------------------------------------------------------
products_id | products_customizer_id | date_added
products_description
----------------------------------------------------------------
id | language_id | sku | description
我想使用 getter 和 setter...我遇到这样的问题:我应该有 1 个产品模型,还是一个用于描述,另一个用于描述产品?
更新 需要明确的是,我被迫使用这个数据库结构...我想知道如何设置我的 php 模型...我希望能够做到...
$product->setCustomizerId(4);
$product->setSku('BJKDJ423');
$product->setDescription('A spanish walrus');
$product->save();
并让它智能地保存每个数据库中的每个字段....这是一个好/坏主意,为什么或为什么不呢?
I'm curious what a good model structure is for a multilingual environment. I'll use a products database as an example....
products
----------------------------------------------------------------
products_id | products_customizer_id | date_added
products_description
----------------------------------------------------------------
id | language_id | sku | description
I'd like to use getters and setters... I'm stuck with the question of, should I have 1 product model, or one for the description and other for products?
update
To be clear, I am forced to use this databse structure... I'm wondering how to setup my php models... I'd like to be able to do...
$product->setCustomizerId(4);
$product->setSku('BJKDJ423');
$product->setDescription('A spanish walrus');
$product->save();
And have it intelligently save each field in each database.... is this a good/bad idea, why or why not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与我的做法相差不远...我倾向于坚持一种格式,比如让所有特定于语言的表格都以
_lang
为后缀。所以我有 3 个表(使用 InnoDB 和外键来保证引用完整性)。我可能会使用 ISO 字符串作为语言 id,例如
en-GB
并使用自动增量 INT 作为产品 id。product_lang
表中的主键将是从language_id
和product_id
字段创建的复合键 - 这样您就可以避免不必要的字段和任何INT 约束本来就有。否则,是的,你已经差不多了。
---- 编辑以反映更新的问题 ----
好的 - 模型...
您的模型应该反映您将如何在业务意义上而不是数据库意义上使用它...我有一个产品模型包装数据,因为你将使用它,所以类似于:
我会在数据库连接器中设置语言,如果你不经常更改它,可能是静态的,并从表中提取相关数据来传播你的对象...我不太相信活跃记录-创建映射业务对象的对象模型和以关系、规范化形式保存数据的数据库。使用数据库抽象层将一种转换为另一种。
除了在 i18n 设置中加载
通常,我会将某些站点范围的变量存储在每个页面上加载的单例应用程序对象中 - 这就是我存储语言 ID 和所有应用程序启动的位置东西,所以它将连接到数据库,启动会话,实例化用户对象,为他们分配购物车等等 - 全局使用的一切。
由于语言将在该
MyApp
对象中设置,并且该对象还将保存您的数据库连接器 - 您只需让该对象设置静态MyDBConnector::setLang($sLang)
实例化时自动参数。这样,要在任何页面上启动整个应用程序,您只需执行类似的操作
That's not too far off how I'd do it... I tend to stick to a format though like having all language specific tabled suffixed with
_lang
. So I'd have the 3 tables (using InnoDB with foreign keys for referential integrity).I'd probably use an ISO string for the language id, for example
en-GB
and an autoincremented INT for the product id. The primary key in theproduct_lang
table would be a compound key created from thelanguage_id
andproduct_id
fields - that way you avoid an unnecessary field and any INT constraints that would have.Otherwise yeah, you're pretty much there.
---- EDIT TO REFLECT UPDATED QUESTION ----
OK - the model...
Your model should reflect how you'll be using it in a business sense rather than a database sense... I'd have a product model that wraps the data as you'll use it so something like:
I'd set the language in the database connector, probably statically if you're not changing it often, and have that pull the relevant data from the tables to propagate your object... I'm not a huge believer in active records - create object models that map business objects and a database that holds the data in a relational, normalised form. Use a database abstraction layer to convert one to the other.
Aside on loading in the i18n settings
Generally I'd store certain site-wide variables in a Singleton application object that's loaded up on every page - that's where I'd store the language id and all the application bootup stuff, so it'll connect to the database, start the session, instantiate a user object, assign them a cart and so on and so forth - everything that's used globally.
Since the language will be set in that
MyApp
object and that object will also hold your database connector - you can simply have that object set the staticMyDBConnector::setLang($sLang)
parameter automatically on instantiation.This way, to kick up the entire application on any page, you need simply do something like
我使用完全灵活的模型结构,将描述、名称、标题公开为模型中的数组,例如:
对所有属性进行清洗和重复。
注意:设置“setNames(array $values)”版本几乎没有什么用处,仅应创建 setName($lang, $value) IMO。
I use a completly flexible model structure that exposes the description, name, title as an array in my models such as:
Wash rince and repeat for all properties.
Note: There is little use to setup a "setNames(array $values)" version, only a setName($lang, $value) should be created IMO.